<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a name="line.1"></a>
<span class="sourceLineNo">002</span>/*<a name="line.2"></a>
<span class="sourceLineNo">003</span> * Copyright (C) 2014 Archie L. Cobbs. All rights reserved.<a name="line.3"></a>
<span class="sourceLineNo">004</span> *<a name="line.4"></a>
<span class="sourceLineNo">005</span> * $Id: Transaction.Callback.html 878 2015-04-17 01:32:03Z archie.cobbs $<a name="line.5"></a>
<span class="sourceLineNo">006</span> */<a name="line.6"></a>
<span class="sourceLineNo">007</span><a name="line.7"></a>
<span class="sourceLineNo">008</span>package org.jsimpledb.core;<a name="line.8"></a>
<span class="sourceLineNo">009</span><a name="line.9"></a>
<span class="sourceLineNo">010</span>import com.google.common.base.Predicate;<a name="line.10"></a>
<span class="sourceLineNo">011</span>import com.google.common.collect.Iterables;<a name="line.11"></a>
<span class="sourceLineNo">012</span>import com.google.common.collect.Maps;<a name="line.12"></a>
<span class="sourceLineNo">013</span><a name="line.13"></a>
<span class="sourceLineNo">014</span>import java.util.ArrayList;<a name="line.14"></a>
<span class="sourceLineNo">015</span>import java.util.Arrays;<a name="line.15"></a>
<span class="sourceLineNo">016</span>import java.util.Collections;<a name="line.16"></a>
<span class="sourceLineNo">017</span>import java.util.HashMap;<a name="line.17"></a>
<span class="sourceLineNo">018</span>import java.util.HashSet;<a name="line.18"></a>
<span class="sourceLineNo">019</span>import java.util.Iterator;<a name="line.19"></a>
<span class="sourceLineNo">020</span>import java.util.LinkedHashSet;<a name="line.20"></a>
<span class="sourceLineNo">021</span>import java.util.List;<a name="line.21"></a>
<span class="sourceLineNo">022</span>import java.util.Map;<a name="line.22"></a>
<span class="sourceLineNo">023</span>import java.util.NavigableMap;<a name="line.23"></a>
<span class="sourceLineNo">024</span>import java.util.NavigableSet;<a name="line.24"></a>
<span class="sourceLineNo">025</span>import java.util.SortedSet;<a name="line.25"></a>
<span class="sourceLineNo">026</span>import java.util.TreeMap;<a name="line.26"></a>
<span class="sourceLineNo">027</span>import java.util.TreeSet;<a name="line.27"></a>
<span class="sourceLineNo">028</span><a name="line.28"></a>
<span class="sourceLineNo">029</span>import org.jsimpledb.kv.KVPair;<a name="line.29"></a>
<span class="sourceLineNo">030</span>import org.jsimpledb.kv.KVTransaction;<a name="line.30"></a>
<span class="sourceLineNo">031</span>import org.jsimpledb.kv.KVTransactionException;<a name="line.31"></a>
<span class="sourceLineNo">032</span>import org.jsimpledb.kv.KeyRanges;<a name="line.32"></a>
<span class="sourceLineNo">033</span>import org.jsimpledb.util.ByteReader;<a name="line.33"></a>
<span class="sourceLineNo">034</span>import org.jsimpledb.util.ByteUtil;<a name="line.34"></a>
<span class="sourceLineNo">035</span>import org.jsimpledb.util.ByteWriter;<a name="line.35"></a>
<span class="sourceLineNo">036</span>import org.jsimpledb.util.NavigableSets;<a name="line.36"></a>
<span class="sourceLineNo">037</span>import org.jsimpledb.util.ObjIdSet;<a name="line.37"></a>
<span class="sourceLineNo">038</span>import org.jsimpledb.util.UnsignedIntEncoder;<a name="line.38"></a>
<span class="sourceLineNo">039</span>import org.slf4j.Logger;<a name="line.39"></a>
<span class="sourceLineNo">040</span>import org.slf4j.LoggerFactory;<a name="line.40"></a>
<span class="sourceLineNo">041</span><a name="line.41"></a>
<span class="sourceLineNo">042</span>/**<a name="line.42"></a>
<span class="sourceLineNo">043</span> * A JSimpleDB {@link Database} transaction.<a name="line.43"></a>
<span class="sourceLineNo">044</span> *<a name="line.44"></a>
<span class="sourceLineNo">045</span> * &lt;p&gt;<a name="line.45"></a>
<span class="sourceLineNo">046</span> * Note: this is the lower level, core API for {@link org.jsimpledb.JSimpleDB}. In most cases this API<a name="line.46"></a>
<span class="sourceLineNo">047</span> * will only be used indirectly through the higher level {@link org.jsimpledb.JSimpleDB}, {@link org.jsimpledb.JTransaction},<a name="line.47"></a>
<span class="sourceLineNo">048</span> * and {@link org.jsimpledb.JObject} APIs.<a name="line.48"></a>
<span class="sourceLineNo">049</span> * &lt;/p&gt;<a name="line.49"></a>
<span class="sourceLineNo">050</span> *<a name="line.50"></a>
<span class="sourceLineNo">051</span> * &lt;p&gt;<a name="line.51"></a>
<span class="sourceLineNo">052</span> * Methods in this class can be divided into the following categories:<a name="line.52"></a>
<span class="sourceLineNo">053</span> * &lt;/p&gt;<a name="line.53"></a>
<span class="sourceLineNo">054</span> *<a name="line.54"></a>
<span class="sourceLineNo">055</span> * &lt;p&gt;<a name="line.55"></a>
<span class="sourceLineNo">056</span> * &lt;b&gt;Transaction Meta-Data&lt;/b&gt;<a name="line.56"></a>
<span class="sourceLineNo">057</span> * &lt;ul&gt;<a name="line.57"></a>
<span class="sourceLineNo">058</span> *  &lt;li&gt;{@link #getDatabase getDatabase()} - Get the associated {@link Database}&lt;/li&gt;<a name="line.58"></a>
<span class="sourceLineNo">059</span> *  &lt;li&gt;{@link #getSchemas getSchemas()} - Get the database {@link Schemas}, as seen by this transaction&lt;/li&gt;<a name="line.59"></a>
<span class="sourceLineNo">060</span> *  &lt;li&gt;{@link #getSchema() getSchema()} - Get the {@link Schema} that will be used by this transaction&lt;/li&gt;<a name="line.60"></a>
<span class="sourceLineNo">061</span> *  &lt;li&gt;{@link #deleteSchemaVersion deleteSchemaVersion()} - Delete a schema version that is no longer being used&lt;/li&gt;<a name="line.61"></a>
<span class="sourceLineNo">062</span> * &lt;/ul&gt;<a name="line.62"></a>
<span class="sourceLineNo">063</span> *<a name="line.63"></a>
<span class="sourceLineNo">064</span> * &lt;p&gt;<a name="line.64"></a>
<span class="sourceLineNo">065</span> * &lt;b&gt;Transaction Lifecycle&lt;/b&gt;<a name="line.65"></a>
<span class="sourceLineNo">066</span> * &lt;ul&gt;<a name="line.66"></a>
<span class="sourceLineNo">067</span> *  &lt;li&gt;{@link #commit commit()} - Commit transaction&lt;/li&gt;<a name="line.67"></a>
<span class="sourceLineNo">068</span> *  &lt;li&gt;{@link #rollback rollback()} - Roll back transaction&lt;/li&gt;<a name="line.68"></a>
<span class="sourceLineNo">069</span> *  &lt;li&gt;{@link #isValid isValid()} - Test transaction validity&lt;/li&gt;<a name="line.69"></a>
<span class="sourceLineNo">070</span> *  &lt;li&gt;{@link #setTimeout setTimeout()} - Set transaction timeout&lt;/li&gt;<a name="line.70"></a>
<span class="sourceLineNo">071</span> *  &lt;li&gt;{@link #setReadOnly setReadOnly()} - Set transaction to read-only&lt;/li&gt;<a name="line.71"></a>
<span class="sourceLineNo">072</span> *  &lt;li&gt;{@link #setRollbackOnly setRollbackOnly()} - Set transaction for rollack only&lt;/li&gt;<a name="line.72"></a>
<span class="sourceLineNo">073</span> *  &lt;li&gt;{@link #addCallback addCallback()} - Register a {@link Callback} on transaction completion&lt;/li&gt;<a name="line.73"></a>
<span class="sourceLineNo">074</span> *  &lt;li&gt;{@link #createSnapshotTransaction createSnapshotTransaction()} - Create a empty, in-memory copy of this transaction&lt;/li&gt;<a name="line.74"></a>
<span class="sourceLineNo">075</span> * &lt;/ul&gt;<a name="line.75"></a>
<span class="sourceLineNo">076</span> *<a name="line.76"></a>
<span class="sourceLineNo">077</span> * &lt;p&gt;<a name="line.77"></a>
<span class="sourceLineNo">078</span> * &lt;b&gt;Object Lifecycle&lt;/b&gt;<a name="line.78"></a>
<span class="sourceLineNo">079</span> * &lt;ul&gt;<a name="line.79"></a>
<span class="sourceLineNo">080</span> *  &lt;li&gt;{@link #create(int) create()} - Create a database object&lt;/li&gt;<a name="line.80"></a>
<span class="sourceLineNo">081</span> *  &lt;li&gt;{@link #delete delete()} - Delete a database object&lt;/li&gt;<a name="line.81"></a>
<span class="sourceLineNo">082</span> *  &lt;li&gt;{@link #exists exists()} - Test whether a database object exists&lt;/li&gt;<a name="line.82"></a>
<span class="sourceLineNo">083</span> *  &lt;li&gt;{@link #copy copy()} - Copy an object's fields onto another object in a (possibly) different transaction&lt;/li&gt;<a name="line.83"></a>
<span class="sourceLineNo">084</span> *  &lt;li&gt;{@link #addCreateListener addCreateListener()} - Register a {@link CreateListener} for notifications about new objects&lt;/li&gt;<a name="line.84"></a>
<span class="sourceLineNo">085</span> *  &lt;li&gt;{@link #removeCreateListener removeCreateListener()} - Unregister a {@link CreateListener}&lt;/li&gt;<a name="line.85"></a>
<span class="sourceLineNo">086</span> *  &lt;li&gt;{@link #addDeleteListener addDeleteListener()} - Register a {@link DeleteListener} for notifications<a name="line.86"></a>
<span class="sourceLineNo">087</span> *      about object deletions&lt;/li&gt;<a name="line.87"></a>
<span class="sourceLineNo">088</span> *  &lt;li&gt;{@link #removeDeleteListener removeDeleteListener()} - Unregister a {@link DeleteListener}&lt;/li&gt;<a name="line.88"></a>
<span class="sourceLineNo">089</span> * &lt;/ul&gt;<a name="line.89"></a>
<span class="sourceLineNo">090</span> *<a name="line.90"></a>
<span class="sourceLineNo">091</span> * &lt;p&gt;<a name="line.91"></a>
<span class="sourceLineNo">092</span> * &lt;b&gt;Object Versioning&lt;/b&gt;<a name="line.92"></a>
<span class="sourceLineNo">093</span> * &lt;ul&gt;<a name="line.93"></a>
<span class="sourceLineNo">094</span> *  &lt;li&gt;{@link #getSchemaVersion(ObjId) getSchemaVersion()} - Inspect an object's schema version&lt;/li&gt;<a name="line.94"></a>
<span class="sourceLineNo">095</span> *  &lt;li&gt;{@link #updateSchemaVersion updateSchemaVersion()} - Update an object's schema version to match this transaction&lt;/li&gt;<a name="line.95"></a>
<span class="sourceLineNo">096</span> *  &lt;li&gt;{@link #queryVersion queryVersion()} - Locate objects by schema version&lt;/li&gt;<a name="line.96"></a>
<span class="sourceLineNo">097</span> *  &lt;li&gt;{@link #addVersionChangeListener addVersionChangeListener()} - Register a {@link VersionChangeListener}<a name="line.97"></a>
<span class="sourceLineNo">098</span> *      for notifications about object version changes&lt;/li&gt;<a name="line.98"></a>
<span class="sourceLineNo">099</span> *  &lt;li&gt;{@link #removeVersionChangeListener removeVersionChangeListener()} - Unregister a {@link VersionChangeListener}&lt;/li&gt;<a name="line.99"></a>
<span class="sourceLineNo">100</span> * &lt;/ul&gt;<a name="line.100"></a>
<span class="sourceLineNo">101</span> *<a name="line.101"></a>
<span class="sourceLineNo">102</span> * &lt;p&gt;<a name="line.102"></a>
<span class="sourceLineNo">103</span> * &lt;b&gt;Object and Field Access&lt;/b&gt;<a name="line.103"></a>
<span class="sourceLineNo">104</span> * &lt;ul&gt;<a name="line.104"></a>
<span class="sourceLineNo">105</span> *  &lt;li&gt;{@link #getAll getAll(int)} - Get all objects, or all objects of a specific type&lt;/li&gt;<a name="line.105"></a>
<span class="sourceLineNo">106</span> *  &lt;li&gt;{@link #readSimpleField readSimpleField()} - Read the value of a {@link SimpleField} in an object&lt;/li&gt;<a name="line.106"></a>
<span class="sourceLineNo">107</span> *  &lt;li&gt;{@link #writeSimpleField writeSimpleField()} - Write the value of a {@link SimpleField} in an object&lt;/li&gt;<a name="line.107"></a>
<span class="sourceLineNo">108</span> *  &lt;li&gt;{@link #readCounterField readCounterField()} - Read the value of a {@link CounterField} in an object&lt;/li&gt;<a name="line.108"></a>
<span class="sourceLineNo">109</span> *  &lt;li&gt;{@link #writeCounterField writeCounterField()} - Write the value of a {@link CounterField} in an object&lt;/li&gt;<a name="line.109"></a>
<span class="sourceLineNo">110</span> *  &lt;li&gt;{@link #adjustCounterField adjustCounterField()} - Adjust the value of a {@link CounterField} in an object&lt;/li&gt;<a name="line.110"></a>
<span class="sourceLineNo">111</span> *  &lt;li&gt;{@link #readSetField readSetField()} - Access a {@link SetField} in an object as a {@link NavigableSet}&lt;/li&gt;<a name="line.111"></a>
<span class="sourceLineNo">112</span> *  &lt;li&gt;{@link #readListField readListField()} - Access a {@link ListField} in an object as a {@link List}&lt;/li&gt;<a name="line.112"></a>
<span class="sourceLineNo">113</span> *  &lt;li&gt;{@link #readMapField readMapField()} - Access a {@link MapField} in an object as a {@link NavigableMap}&lt;/li&gt;<a name="line.113"></a>
<span class="sourceLineNo">114</span> *  &lt;li&gt;{@link #getKey getKey(ObjId)} - Get the {@link org.jsimpledb.kv.KVDatabase} key corresponding to an object&lt;/li&gt;<a name="line.114"></a>
<span class="sourceLineNo">115</span> *  &lt;li&gt;{@link #getKey getKey(ObjId, int)} - Get the {@link org.jsimpledb.kv.KVDatabase}<a name="line.115"></a>
<span class="sourceLineNo">116</span> *      key corresponding to a field in an object&lt;/li&gt;<a name="line.116"></a>
<span class="sourceLineNo">117</span> * &lt;/ul&gt;<a name="line.117"></a>
<span class="sourceLineNo">118</span> *<a name="line.118"></a>
<span class="sourceLineNo">119</span> * &lt;p&gt;<a name="line.119"></a>
<span class="sourceLineNo">120</span> * &lt;b&gt;Field Change Notifications&lt;/b&gt;<a name="line.120"></a>
<span class="sourceLineNo">121</span> * &lt;ul&gt;<a name="line.121"></a>
<span class="sourceLineNo">122</span> *  &lt;li&gt;{@link #addSimpleFieldChangeListener addSimpleFieldChangeListener()} - Register a {@link SimpleFieldChangeListener} for<a name="line.122"></a>
<span class="sourceLineNo">123</span> *      notifications of changes in a {@link SimpleField}, as seen through a path of object references&lt;/li&gt;<a name="line.123"></a>
<span class="sourceLineNo">124</span> *  &lt;li&gt;{@link #addSetFieldChangeListener addSetFieldChangeListener()} - Register a {@link SetFieldChangeListener} for<a name="line.124"></a>
<span class="sourceLineNo">125</span> *      notifications of changes in a {@link SetField}, as seen through a path of object references&lt;/li&gt;<a name="line.125"></a>
<span class="sourceLineNo">126</span> *  &lt;li&gt;{@link #addListFieldChangeListener addListFieldChangeListener()} - Register a {@link ListFieldChangeListener} for<a name="line.126"></a>
<span class="sourceLineNo">127</span> *      notifications of changes in a {@link ListField}, as seen through a path of object references&lt;/li&gt;<a name="line.127"></a>
<span class="sourceLineNo">128</span> *  &lt;li&gt;{@link #addMapFieldChangeListener addMapFieldChangeListener()} - Register a {@link MapFieldChangeListener} for<a name="line.128"></a>
<span class="sourceLineNo">129</span> *      notifications of changes in a {@link MapField}, as seen through a path of object references&lt;/li&gt;<a name="line.129"></a>
<span class="sourceLineNo">130</span> *  &lt;li&gt;{@link #removeSimpleFieldChangeListener removeSimpleFieldChangeListener()} - Unregister a previously registered<a name="line.130"></a>
<span class="sourceLineNo">131</span> *      {@link SimpleFieldChangeListener}&lt;/li&gt;<a name="line.131"></a>
<span class="sourceLineNo">132</span> *  &lt;li&gt;{@link #removeSetFieldChangeListener removeSetFieldChangeListener()} - Unregister a previously registered<a name="line.132"></a>
<span class="sourceLineNo">133</span> *      {@link SetFieldChangeListener}&lt;/li&gt;<a name="line.133"></a>
<span class="sourceLineNo">134</span> *  &lt;li&gt;{@link #removeListFieldChangeListener removeListFieldChangeListener()} - Unregister a previously registered<a name="line.134"></a>
<span class="sourceLineNo">135</span> *      {@link ListFieldChangeListener}&lt;/li&gt;<a name="line.135"></a>
<span class="sourceLineNo">136</span> *  &lt;li&gt;{@link #removeMapFieldChangeListener removeMapFieldChangeListener()} - Unregister a previously registered<a name="line.136"></a>
<span class="sourceLineNo">137</span> *      {@link MapFieldChangeListener}&lt;/li&gt;<a name="line.137"></a>
<span class="sourceLineNo">138</span> * &lt;/ul&gt;<a name="line.138"></a>
<span class="sourceLineNo">139</span> *<a name="line.139"></a>
<span class="sourceLineNo">140</span> * &lt;p&gt;<a name="line.140"></a>
<span class="sourceLineNo">141</span> * &lt;b&gt;Reference Inversion&lt;/b&gt;<a name="line.141"></a>
<span class="sourceLineNo">142</span> * &lt;ul&gt;<a name="line.142"></a>
<span class="sourceLineNo">143</span> *  &lt;li&gt;{@link #invertReferencePath invertReferencePath()} - Find all objects that refer to any element in a given set<a name="line.143"></a>
<span class="sourceLineNo">144</span> *      of objects through a specified reference path&lt;/li&gt;<a name="line.144"></a>
<span class="sourceLineNo">145</span> * &lt;/ul&gt;<a name="line.145"></a>
<span class="sourceLineNo">146</span> *<a name="line.146"></a>
<span class="sourceLineNo">147</span> * &lt;p&gt;<a name="line.147"></a>
<span class="sourceLineNo">148</span> * &lt;b&gt;Index Queries&lt;/b&gt;<a name="line.148"></a>
<span class="sourceLineNo">149</span> * &lt;ul&gt;<a name="line.149"></a>
<span class="sourceLineNo">150</span> *  &lt;li&gt;{@link #queryIndex queryIndex()} - Query the index associated with a {@link SimpleField}<a name="line.150"></a>
<span class="sourceLineNo">151</span> *      to identify all values and all objects having those values&lt;/li&gt;<a name="line.151"></a>
<span class="sourceLineNo">152</span> *  &lt;li&gt;{@link #queryListElementIndex queryListElementIndex()} - Query the index associated with a {@link ListField}<a name="line.152"></a>
<span class="sourceLineNo">153</span> *      to identify all list elements, all objects having those elements in the list, and thier corresponding indicies&lt;/li&gt;<a name="line.153"></a>
<span class="sourceLineNo">154</span> *  &lt;li&gt;{@link #queryMapValueIndex queryMapValueIndex()} - Query the index associated with a {@link MapField}<a name="line.154"></a>
<span class="sourceLineNo">155</span> *      to identify all map values, all objects having those values in the map, and the corresponding keys&lt;/li&gt;<a name="line.155"></a>
<span class="sourceLineNo">156</span> *  &lt;li&gt;{@link #queryCompositeIndex queryCompositeIndex()} - Query any composite index&lt;/li&gt;<a name="line.156"></a>
<span class="sourceLineNo">157</span> *  &lt;li&gt;{@link #queryCompositeIndex2 queryCompositeIndex2()} - Query a composite index on two fields&lt;/li&gt;<a name="line.157"></a>
<span class="sourceLineNo">158</span> *  &lt;li&gt;{@link #queryCompositeIndex3 queryCompositeIndex3()} - Query a composite index on three fields&lt;/li&gt;<a name="line.158"></a>
<span class="sourceLineNo">159</span> *  &lt;li&gt;{@link #queryCompositeIndex3 queryCompositeIndex4()} - Query a composite index on four fields&lt;/li&gt;<a name="line.159"></a>
<span class="sourceLineNo">160</span> *  &lt;!-- COMPOSITE-INDEX --&gt;<a name="line.160"></a>
<span class="sourceLineNo">161</span> * &lt;/ul&gt;<a name="line.161"></a>
<span class="sourceLineNo">162</span> *<a name="line.162"></a>
<span class="sourceLineNo">163</span> * &lt;p&gt;<a name="line.163"></a>
<span class="sourceLineNo">164</span> * All methods returning a set of values return a {@link NavigableSet}.<a name="line.164"></a>
<span class="sourceLineNo">165</span> * The {@link NavigableSets} utility class provides methods for the efficient {@link NavigableSets#intersection intersection},<a name="line.165"></a>
<span class="sourceLineNo">166</span> * {@link NavigableSets#union union}, {@link NavigableSets#difference difference}, and<a name="line.166"></a>
<span class="sourceLineNo">167</span> * {@link NavigableSets#symmetricDifference symmetric difference} of {@link NavigableSet}s containing the same element type,<a name="line.167"></a>
<span class="sourceLineNo">168</span> * thereby providing the equivalent of traditional database joins.<a name="line.168"></a>
<span class="sourceLineNo">169</span> * &lt;/p&gt;<a name="line.169"></a>
<span class="sourceLineNo">170</span> */<a name="line.170"></a>
<span class="sourceLineNo">171</span>public class Transaction {<a name="line.171"></a>
<span class="sourceLineNo">172</span><a name="line.172"></a>
<span class="sourceLineNo">173</span>    private static final int MAX_GENERATED_KEY_ATTEMPTS = 1000;<a name="line.173"></a>
<span class="sourceLineNo">174</span><a name="line.174"></a>
<span class="sourceLineNo">175</span>    protected final Logger log = LoggerFactory.getLogger(this.getClass());<a name="line.175"></a>
<span class="sourceLineNo">176</span><a name="line.176"></a>
<span class="sourceLineNo">177</span>    final Database db;<a name="line.177"></a>
<span class="sourceLineNo">178</span>    final KVTransaction kvt;<a name="line.178"></a>
<span class="sourceLineNo">179</span>    final Schemas schemas;<a name="line.179"></a>
<span class="sourceLineNo">180</span>    final Schema schema;<a name="line.180"></a>
<span class="sourceLineNo">181</span><a name="line.181"></a>
<span class="sourceLineNo">182</span>    boolean stale;<a name="line.182"></a>
<span class="sourceLineNo">183</span>    boolean readOnly;<a name="line.183"></a>
<span class="sourceLineNo">184</span>    boolean rollbackOnly;<a name="line.184"></a>
<span class="sourceLineNo">185</span><a name="line.185"></a>
<span class="sourceLineNo">186</span>    private final ThreadLocal&lt;TreeMap&lt;Integer, ArrayList&lt;FieldChangeNotifier&gt;&gt;&gt; pendingNotifications = new ThreadLocal&lt;&gt;();<a name="line.186"></a>
<span class="sourceLineNo">187</span>    private final HashSet&lt;VersionChangeListener&gt; versionChangeListeners = new HashSet&lt;&gt;();<a name="line.187"></a>
<span class="sourceLineNo">188</span>    private final HashSet&lt;CreateListener&gt; createListeners = new HashSet&lt;&gt;();<a name="line.188"></a>
<span class="sourceLineNo">189</span>    private final HashSet&lt;DeleteListener&gt; deleteListeners = new HashSet&lt;&gt;();<a name="line.189"></a>
<span class="sourceLineNo">190</span>    private final TreeMap&lt;Integer, HashSet&lt;FieldMonitor&gt;&gt; monitorMap = new TreeMap&lt;&gt;();<a name="line.190"></a>
<span class="sourceLineNo">191</span>    private final LinkedHashSet&lt;Callback&gt; callbacks = new LinkedHashSet&lt;&gt;();<a name="line.191"></a>
<span class="sourceLineNo">192</span><a name="line.192"></a>
<span class="sourceLineNo">193</span>    Transaction(Database db, KVTransaction kvt, Schemas schemas, int versionNumber) {<a name="line.193"></a>
<span class="sourceLineNo">194</span>        this(db, kvt, schemas, schemas.getVersion(versionNumber));<a name="line.194"></a>
<span class="sourceLineNo">195</span>    }<a name="line.195"></a>
<span class="sourceLineNo">196</span><a name="line.196"></a>
<span class="sourceLineNo">197</span>    Transaction(Database db, KVTransaction kvt, Schemas schemas, Schema schema) {<a name="line.197"></a>
<span class="sourceLineNo">198</span>        this.db = db;<a name="line.198"></a>
<span class="sourceLineNo">199</span>        this.kvt = kvt;<a name="line.199"></a>
<span class="sourceLineNo">200</span>        this.schemas = schemas;<a name="line.200"></a>
<span class="sourceLineNo">201</span>        this.schema = schema;<a name="line.201"></a>
<span class="sourceLineNo">202</span>    }<a name="line.202"></a>
<span class="sourceLineNo">203</span><a name="line.203"></a>
<span class="sourceLineNo">204</span>// Transaction Meta-Data<a name="line.204"></a>
<span class="sourceLineNo">205</span><a name="line.205"></a>
<span class="sourceLineNo">206</span>    /**<a name="line.206"></a>
<span class="sourceLineNo">207</span>     * Get the database with which this transaction is associated.<a name="line.207"></a>
<span class="sourceLineNo">208</span>     *<a name="line.208"></a>
<span class="sourceLineNo">209</span>     * @return associated database<a name="line.209"></a>
<span class="sourceLineNo">210</span>     */<a name="line.210"></a>
<span class="sourceLineNo">211</span>    public Database getDatabase() {<a name="line.211"></a>
<span class="sourceLineNo">212</span>        return this.db;<a name="line.212"></a>
<span class="sourceLineNo">213</span>    }<a name="line.213"></a>
<span class="sourceLineNo">214</span><a name="line.214"></a>
<span class="sourceLineNo">215</span>    /**<a name="line.215"></a>
<span class="sourceLineNo">216</span>     * Get the database schema versions known to this transaction.<a name="line.216"></a>
<span class="sourceLineNo">217</span>     * This reflects all schema versions currently recorded in the database.<a name="line.217"></a>
<span class="sourceLineNo">218</span>     *<a name="line.218"></a>
<span class="sourceLineNo">219</span>     * @return associated schemas<a name="line.219"></a>
<span class="sourceLineNo">220</span>     */<a name="line.220"></a>
<span class="sourceLineNo">221</span>    public Schemas getSchemas() {<a name="line.221"></a>
<span class="sourceLineNo">222</span>        return this.schemas;<a name="line.222"></a>
<span class="sourceLineNo">223</span>    }<a name="line.223"></a>
<span class="sourceLineNo">224</span><a name="line.224"></a>
<span class="sourceLineNo">225</span>    /**<a name="line.225"></a>
<span class="sourceLineNo">226</span>     * Get the database schema version associated with this transaction.<a name="line.226"></a>
<span class="sourceLineNo">227</span>     * This is the schema version used for newly created objects, and the target schema version when<a name="line.227"></a>
<span class="sourceLineNo">228</span>     * {@linkplain #updateSchemaVersion upgrading} objects.<a name="line.228"></a>
<span class="sourceLineNo">229</span>     *<a name="line.229"></a>
<span class="sourceLineNo">230</span>     * @return associated schema<a name="line.230"></a>
<span class="sourceLineNo">231</span>     */<a name="line.231"></a>
<span class="sourceLineNo">232</span>    public Schema getSchema() {<a name="line.232"></a>
<span class="sourceLineNo">233</span>        return this.schema;<a name="line.233"></a>
<span class="sourceLineNo">234</span>    }<a name="line.234"></a>
<span class="sourceLineNo">235</span><a name="line.235"></a>
<span class="sourceLineNo">236</span>    /**<a name="line.236"></a>
<span class="sourceLineNo">237</span>     * Delete a schema version that is no longer being used. There must be no objects with the given version<a name="line.237"></a>
<span class="sourceLineNo">238</span>     * in the database, and {@code version} must not be the version being used by this transaction.<a name="line.238"></a>
<span class="sourceLineNo">239</span>     *<a name="line.239"></a>
<span class="sourceLineNo">240</span>     * @param version schema version to remove<a name="line.240"></a>
<span class="sourceLineNo">241</span>     * @return true if schema version was found and removed, false if schema version does not exist in database<a name="line.241"></a>
<span class="sourceLineNo">242</span>     * @throws InvalidSchemaException if one or more objects with schema version {@code version} still exist<a name="line.242"></a>
<span class="sourceLineNo">243</span>     * @throws InvalidSchemaException if {@code version} is equal to this transaction's version<a name="line.243"></a>
<span class="sourceLineNo">244</span>     * @throws IllegalArgumentException if {@code version} is zero or negative<a name="line.244"></a>
<span class="sourceLineNo">245</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.245"></a>
<span class="sourceLineNo">246</span>     */<a name="line.246"></a>
<span class="sourceLineNo">247</span>    public synchronized boolean deleteSchemaVersion(int version) {<a name="line.247"></a>
<span class="sourceLineNo">248</span><a name="line.248"></a>
<span class="sourceLineNo">249</span>        // Sanity check<a name="line.249"></a>
<span class="sourceLineNo">250</span>        if (version &lt;= 0)<a name="line.250"></a>
<span class="sourceLineNo">251</span>            throw new IllegalArgumentException("invalid schema version " + version);<a name="line.251"></a>
<span class="sourceLineNo">252</span>        if (version == this.schema.getVersionNumber())<a name="line.252"></a>
<span class="sourceLineNo">253</span>            throw new InvalidSchemaException("version " + version + " is this transaction's version");<a name="line.253"></a>
<span class="sourceLineNo">254</span>        if (this.stale)<a name="line.254"></a>
<span class="sourceLineNo">255</span>            throw new StaleTransactionException(this);<a name="line.255"></a>
<span class="sourceLineNo">256</span>        if (this.queryVersion().asMap().containsKey(version))<a name="line.256"></a>
<span class="sourceLineNo">257</span>            throw new InvalidSchemaException("one or more version " + version + " objects still exist in database");<a name="line.257"></a>
<span class="sourceLineNo">258</span><a name="line.258"></a>
<span class="sourceLineNo">259</span>        // Delete schema version<a name="line.259"></a>
<span class="sourceLineNo">260</span>        if (!this.schemas.deleteVersion(version))<a name="line.260"></a>
<span class="sourceLineNo">261</span>            return false;<a name="line.261"></a>
<span class="sourceLineNo">262</span>        this.db.deleteSchema(this.kvt, version);<a name="line.262"></a>
<span class="sourceLineNo">263</span>        return true;<a name="line.263"></a>
<span class="sourceLineNo">264</span>    }<a name="line.264"></a>
<span class="sourceLineNo">265</span><a name="line.265"></a>
<span class="sourceLineNo">266</span>    /**<a name="line.266"></a>
<span class="sourceLineNo">267</span>     * Get the underlying key/value store transaction.<a name="line.267"></a>
<span class="sourceLineNo">268</span>     *<a name="line.268"></a>
<span class="sourceLineNo">269</span>     * &lt;p&gt;<a name="line.269"></a>
<span class="sourceLineNo">270</span>     * &lt;b&gt;Warning:&lt;/b&gt; making changes to the key/value store directly is not supported. If any changes<a name="line.270"></a>
<span class="sourceLineNo">271</span>     * are made, all future behavior is undefined.<a name="line.271"></a>
<span class="sourceLineNo">272</span>     * &lt;/p&gt;<a name="line.272"></a>
<span class="sourceLineNo">273</span>     *<a name="line.273"></a>
<span class="sourceLineNo">274</span>     * @return the associated key/value transaction<a name="line.274"></a>
<span class="sourceLineNo">275</span>     */<a name="line.275"></a>
<span class="sourceLineNo">276</span>    public KVTransaction getKVTransaction() {<a name="line.276"></a>
<span class="sourceLineNo">277</span>        return this.kvt;<a name="line.277"></a>
<span class="sourceLineNo">278</span>    }<a name="line.278"></a>
<span class="sourceLineNo">279</span><a name="line.279"></a>
<span class="sourceLineNo">280</span>// Transaction Lifecycle<a name="line.280"></a>
<span class="sourceLineNo">281</span><a name="line.281"></a>
<span class="sourceLineNo">282</span>    /**<a name="line.282"></a>
<span class="sourceLineNo">283</span>     * Commit this transaction.<a name="line.283"></a>
<span class="sourceLineNo">284</span>     *<a name="line.284"></a>
<span class="sourceLineNo">285</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.285"></a>
<span class="sourceLineNo">286</span>     * @throws org.jsimpledb.kv.RetryTransactionException from {@link KVTransaction#commit KVTransaction.commit()}<a name="line.286"></a>
<span class="sourceLineNo">287</span>     * @throws RollbackOnlyTransactionException if this instance has been {@linkplain #setRollbackOnly marked} rollback only;<a name="line.287"></a>
<span class="sourceLineNo">288</span>     *  this instance will be automatically rolled back<a name="line.288"></a>
<span class="sourceLineNo">289</span>     */<a name="line.289"></a>
<span class="sourceLineNo">290</span>    public synchronized void commit() {<a name="line.290"></a>
<span class="sourceLineNo">291</span><a name="line.291"></a>
<span class="sourceLineNo">292</span>        // Sanity check<a name="line.292"></a>
<span class="sourceLineNo">293</span>        if (this.stale)<a name="line.293"></a>
<span class="sourceLineNo">294</span>            throw new StaleTransactionException(this);<a name="line.294"></a>
<span class="sourceLineNo">295</span><a name="line.295"></a>
<span class="sourceLineNo">296</span>        // Rollback only?<a name="line.296"></a>
<span class="sourceLineNo">297</span>        if (this.rollbackOnly) {<a name="line.297"></a>
<span class="sourceLineNo">298</span>            this.log.debug("commit() invoked on transaction " + this + " marked rollback-only, rolling back");<a name="line.298"></a>
<span class="sourceLineNo">299</span>            this.rollback();<a name="line.299"></a>
<span class="sourceLineNo">300</span>            throw new RollbackOnlyTransactionException(this);<a name="line.300"></a>
<span class="sourceLineNo">301</span>        }<a name="line.301"></a>
<span class="sourceLineNo">302</span>        this.stale = true;<a name="line.302"></a>
<span class="sourceLineNo">303</span><a name="line.303"></a>
<span class="sourceLineNo">304</span>        // Do before completion callbacks<a name="line.304"></a>
<span class="sourceLineNo">305</span>        if (this.log.isTraceEnabled())<a name="line.305"></a>
<span class="sourceLineNo">306</span>            this.log.trace("commit() invoked on" + (this.readOnly ? " read-only" : "") + " transaction " + this);<a name="line.306"></a>
<span class="sourceLineNo">307</span>        Callback failedCallback = null;<a name="line.307"></a>
<span class="sourceLineNo">308</span>        try {<a name="line.308"></a>
<span class="sourceLineNo">309</span>            for (Callback callback : this.callbacks) {<a name="line.309"></a>
<span class="sourceLineNo">310</span>                failedCallback = callback;<a name="line.310"></a>
<span class="sourceLineNo">311</span>                callback.beforeCommit(this.readOnly);<a name="line.311"></a>
<span class="sourceLineNo">312</span>            }<a name="line.312"></a>
<span class="sourceLineNo">313</span>            failedCallback = null;<a name="line.313"></a>
<span class="sourceLineNo">314</span>        } finally {<a name="line.314"></a>
<span class="sourceLineNo">315</span>            if (failedCallback != null) {<a name="line.315"></a>
<span class="sourceLineNo">316</span>                this.log.warn("error invoking beforeCommit() method on transaction "<a name="line.316"></a>
<span class="sourceLineNo">317</span>                  + this + " callback " + failedCallback + ", rolling back");<a name="line.317"></a>
<span class="sourceLineNo">318</span>            }<a name="line.318"></a>
<span class="sourceLineNo">319</span>            this.triggerBeforeCompletion();<a name="line.319"></a>
<span class="sourceLineNo">320</span>            if (failedCallback != null) {<a name="line.320"></a>
<span class="sourceLineNo">321</span>                try {<a name="line.321"></a>
<span class="sourceLineNo">322</span>                    try {<a name="line.322"></a>
<span class="sourceLineNo">323</span>                        this.kvt.rollback();<a name="line.323"></a>
<span class="sourceLineNo">324</span>                    } catch (KVTransactionException e) {<a name="line.324"></a>
<span class="sourceLineNo">325</span>                        // ignore<a name="line.325"></a>
<span class="sourceLineNo">326</span>                    }<a name="line.326"></a>
<span class="sourceLineNo">327</span>                } finally {<a name="line.327"></a>
<span class="sourceLineNo">328</span>                    this.triggerAfterCompletion(false);<a name="line.328"></a>
<span class="sourceLineNo">329</span>                }<a name="line.329"></a>
<span class="sourceLineNo">330</span>            }<a name="line.330"></a>
<span class="sourceLineNo">331</span>        }<a name="line.331"></a>
<span class="sourceLineNo">332</span><a name="line.332"></a>
<span class="sourceLineNo">333</span>        // Commit KVTransaction and trigger after completion callbacks<a name="line.333"></a>
<span class="sourceLineNo">334</span>        failedCallback = null;<a name="line.334"></a>
<span class="sourceLineNo">335</span>        try {<a name="line.335"></a>
<span class="sourceLineNo">336</span>            this.kvt.commit();<a name="line.336"></a>
<span class="sourceLineNo">337</span>            for (Callback callback : this.callbacks) {<a name="line.337"></a>
<span class="sourceLineNo">338</span>                failedCallback = callback;<a name="line.338"></a>
<span class="sourceLineNo">339</span>                callback.afterCommit();<a name="line.339"></a>
<span class="sourceLineNo">340</span>            }<a name="line.340"></a>
<span class="sourceLineNo">341</span>            failedCallback = null;<a name="line.341"></a>
<span class="sourceLineNo">342</span>        } finally {<a name="line.342"></a>
<span class="sourceLineNo">343</span>            if (failedCallback != null)<a name="line.343"></a>
<span class="sourceLineNo">344</span>                this.log.warn("error invoking afterCommit() method on transaction " + this + " callback " + failedCallback);<a name="line.344"></a>
<span class="sourceLineNo">345</span>            this.triggerAfterCompletion(true);<a name="line.345"></a>
<span class="sourceLineNo">346</span>        }<a name="line.346"></a>
<span class="sourceLineNo">347</span>    }<a name="line.347"></a>
<span class="sourceLineNo">348</span><a name="line.348"></a>
<span class="sourceLineNo">349</span>    /**<a name="line.349"></a>
<span class="sourceLineNo">350</span>     * Roll back this transaction.<a name="line.350"></a>
<span class="sourceLineNo">351</span>     *<a name="line.351"></a>
<span class="sourceLineNo">352</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.352"></a>
<span class="sourceLineNo">353</span>     */<a name="line.353"></a>
<span class="sourceLineNo">354</span>    public synchronized void rollback() {<a name="line.354"></a>
<span class="sourceLineNo">355</span><a name="line.355"></a>
<span class="sourceLineNo">356</span>        // Sanity check<a name="line.356"></a>
<span class="sourceLineNo">357</span>        if (this.stale)<a name="line.357"></a>
<span class="sourceLineNo">358</span>            throw new StaleTransactionException(this);<a name="line.358"></a>
<span class="sourceLineNo">359</span>        this.stale = true;<a name="line.359"></a>
<span class="sourceLineNo">360</span>        if (this.log.isTraceEnabled())<a name="line.360"></a>
<span class="sourceLineNo">361</span>            this.log.trace("rollback() invoked on" + (this.readOnly ? " read-only" : "") + " transaction " + this);<a name="line.361"></a>
<span class="sourceLineNo">362</span><a name="line.362"></a>
<span class="sourceLineNo">363</span>        // Do before completion callbacks<a name="line.363"></a>
<span class="sourceLineNo">364</span>        this.triggerBeforeCompletion();<a name="line.364"></a>
<span class="sourceLineNo">365</span><a name="line.365"></a>
<span class="sourceLineNo">366</span>        // Roll back KVTransaction and trigger after completion callbacks<a name="line.366"></a>
<span class="sourceLineNo">367</span>        try {<a name="line.367"></a>
<span class="sourceLineNo">368</span>            this.kvt.rollback();<a name="line.368"></a>
<span class="sourceLineNo">369</span>        } finally {<a name="line.369"></a>
<span class="sourceLineNo">370</span>            this.triggerAfterCompletion(false);<a name="line.370"></a>
<span class="sourceLineNo">371</span>        }<a name="line.371"></a>
<span class="sourceLineNo">372</span>    }<a name="line.372"></a>
<span class="sourceLineNo">373</span><a name="line.373"></a>
<span class="sourceLineNo">374</span>    private /*synchronized*/ void triggerBeforeCompletion() {<a name="line.374"></a>
<span class="sourceLineNo">375</span>        for (Callback callback : this.callbacks) {<a name="line.375"></a>
<span class="sourceLineNo">376</span>            try {<a name="line.376"></a>
<span class="sourceLineNo">377</span>                callback.beforeCompletion();<a name="line.377"></a>
<span class="sourceLineNo">378</span>            } catch (Throwable t) {<a name="line.378"></a>
<span class="sourceLineNo">379</span>                this.log.error("error invoking beforeCompletion() method on transaction callback " + callback, t);<a name="line.379"></a>
<span class="sourceLineNo">380</span>            }<a name="line.380"></a>
<span class="sourceLineNo">381</span>        }<a name="line.381"></a>
<span class="sourceLineNo">382</span>    }<a name="line.382"></a>
<span class="sourceLineNo">383</span><a name="line.383"></a>
<span class="sourceLineNo">384</span>    private /*synchronized*/ void triggerAfterCompletion(boolean committed) {<a name="line.384"></a>
<span class="sourceLineNo">385</span>        for (Callback callback : this.callbacks) {<a name="line.385"></a>
<span class="sourceLineNo">386</span>            try {<a name="line.386"></a>
<span class="sourceLineNo">387</span>                callback.afterCompletion(committed);<a name="line.387"></a>
<span class="sourceLineNo">388</span>            } catch (Throwable t) {<a name="line.388"></a>
<span class="sourceLineNo">389</span>                this.log.error("error invoking afterCompletion() method on transaction callback " + callback, t);<a name="line.389"></a>
<span class="sourceLineNo">390</span>            }<a name="line.390"></a>
<span class="sourceLineNo">391</span>        }<a name="line.391"></a>
<span class="sourceLineNo">392</span>    }<a name="line.392"></a>
<span class="sourceLineNo">393</span><a name="line.393"></a>
<span class="sourceLineNo">394</span>    /**<a name="line.394"></a>
<span class="sourceLineNo">395</span>     * Determine whether this transaction is still valid. If not, all other methods in this class<a name="line.395"></a>
<span class="sourceLineNo">396</span>     * will throw {@link StaleTransactionException}.<a name="line.396"></a>
<span class="sourceLineNo">397</span>     *<a name="line.397"></a>
<span class="sourceLineNo">398</span>     * @return true if this instance is still usable<a name="line.398"></a>
<span class="sourceLineNo">399</span>     */<a name="line.399"></a>
<span class="sourceLineNo">400</span>    public synchronized boolean isValid() {<a name="line.400"></a>
<span class="sourceLineNo">401</span>        return !this.stale;<a name="line.401"></a>
<span class="sourceLineNo">402</span>    }<a name="line.402"></a>
<span class="sourceLineNo">403</span><a name="line.403"></a>
<span class="sourceLineNo">404</span>    /**<a name="line.404"></a>
<span class="sourceLineNo">405</span>     * Determine whether this transaction is read-only.<a name="line.405"></a>
<span class="sourceLineNo">406</span>     *<a name="line.406"></a>
<span class="sourceLineNo">407</span>     * @return true if this instance is read-only<a name="line.407"></a>
<span class="sourceLineNo">408</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.408"></a>
<span class="sourceLineNo">409</span>     */<a name="line.409"></a>
<span class="sourceLineNo">410</span>    public synchronized boolean isReadOnly() {<a name="line.410"></a>
<span class="sourceLineNo">411</span>        if (this.stale)<a name="line.411"></a>
<span class="sourceLineNo">412</span>            throw new StaleTransactionException(this);<a name="line.412"></a>
<span class="sourceLineNo">413</span>        return this.readOnly;<a name="line.413"></a>
<span class="sourceLineNo">414</span>    }<a name="line.414"></a>
<span class="sourceLineNo">415</span><a name="line.415"></a>
<span class="sourceLineNo">416</span>    /**<a name="line.416"></a>
<span class="sourceLineNo">417</span>     * Enable or disaable read-only mode. When in read-only mode, all mutating operations will fail with<a name="line.417"></a>
<span class="sourceLineNo">418</span>     * a {@link ReadOnlyTransactionException}.<a name="line.418"></a>
<span class="sourceLineNo">419</span>     *<a name="line.419"></a>
<span class="sourceLineNo">420</span>     * @param readOnly read-only setting<a name="line.420"></a>
<span class="sourceLineNo">421</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.421"></a>
<span class="sourceLineNo">422</span>     */<a name="line.422"></a>
<span class="sourceLineNo">423</span>    public synchronized void setReadOnly(boolean readOnly) {<a name="line.423"></a>
<span class="sourceLineNo">424</span>        if (this.stale)<a name="line.424"></a>
<span class="sourceLineNo">425</span>            throw new StaleTransactionException(this);<a name="line.425"></a>
<span class="sourceLineNo">426</span>        this.readOnly = readOnly;<a name="line.426"></a>
<span class="sourceLineNo">427</span>    }<a name="line.427"></a>
<span class="sourceLineNo">428</span><a name="line.428"></a>
<span class="sourceLineNo">429</span>    /**<a name="line.429"></a>
<span class="sourceLineNo">430</span>     * Determine whether this transaction is marked rollback only.<a name="line.430"></a>
<span class="sourceLineNo">431</span>     *<a name="line.431"></a>
<span class="sourceLineNo">432</span>     * @return true if this instance is marked for rollback only<a name="line.432"></a>
<span class="sourceLineNo">433</span>     */<a name="line.433"></a>
<span class="sourceLineNo">434</span>    public synchronized boolean isRollbackOnly() {<a name="line.434"></a>
<span class="sourceLineNo">435</span>        return this.rollbackOnly;<a name="line.435"></a>
<span class="sourceLineNo">436</span>    }<a name="line.436"></a>
<span class="sourceLineNo">437</span><a name="line.437"></a>
<span class="sourceLineNo">438</span>    /**<a name="line.438"></a>
<span class="sourceLineNo">439</span>     * Mark this transaction for rollback only. A subsequent attempt to {@link #commit} will throw an exception.<a name="line.439"></a>
<span class="sourceLineNo">440</span>     */<a name="line.440"></a>
<span class="sourceLineNo">441</span>    public synchronized void setRollbackOnly() {<a name="line.441"></a>
<span class="sourceLineNo">442</span>        this.rollbackOnly = true;<a name="line.442"></a>
<span class="sourceLineNo">443</span>    }<a name="line.443"></a>
<span class="sourceLineNo">444</span><a name="line.444"></a>
<span class="sourceLineNo">445</span>    /**<a name="line.445"></a>
<span class="sourceLineNo">446</span>     * Change the timeout for this transaction from its default value (optional operation).<a name="line.446"></a>
<span class="sourceLineNo">447</span>     *<a name="line.447"></a>
<span class="sourceLineNo">448</span>     * @param timeout transaction timeout in milliseconds, or zero for unlimited<a name="line.448"></a>
<span class="sourceLineNo">449</span>     * @throws UnsupportedOperationException if this transaction does not support timeouts<a name="line.449"></a>
<span class="sourceLineNo">450</span>     * @throws IllegalArgumentException if {@code timeout} is negative<a name="line.450"></a>
<span class="sourceLineNo">451</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.451"></a>
<span class="sourceLineNo">452</span>     */<a name="line.452"></a>
<span class="sourceLineNo">453</span>    public void setTimeout(long timeout) {<a name="line.453"></a>
<span class="sourceLineNo">454</span>        if (this.stale)<a name="line.454"></a>
<span class="sourceLineNo">455</span>            throw new StaleTransactionException(this);<a name="line.455"></a>
<span class="sourceLineNo">456</span>        this.kvt.setTimeout(timeout);<a name="line.456"></a>
<span class="sourceLineNo">457</span>    }<a name="line.457"></a>
<span class="sourceLineNo">458</span><a name="line.458"></a>
<span class="sourceLineNo">459</span>    /**<a name="line.459"></a>
<span class="sourceLineNo">460</span>     * Register a transaction {@link Callback} to be invoked when this transaction completes.<a name="line.460"></a>
<span class="sourceLineNo">461</span>     * Callbacks will be invoked in the order they are registered, but &lt;i&gt;duplicate registrations are ignored&lt;/i&gt;.<a name="line.461"></a>
<span class="sourceLineNo">462</span>     *<a name="line.462"></a>
<span class="sourceLineNo">463</span>     * &lt;p&gt;<a name="line.463"></a>
<span class="sourceLineNo">464</span>     * Note: if you are using Spring for transaction demarcation (via {@link org.jsimpledb.spring.JSimpleDBTransactionManager}),<a name="line.464"></a>
<span class="sourceLineNo">465</span>     * then you may also use Spring's<a name="line.465"></a>
<span class="sourceLineNo">466</span>     * {@link org.springframework.transaction.support.TransactionSynchronizationManager#registerSynchronization<a name="line.466"></a>
<span class="sourceLineNo">467</span>     * TransactionSynchronizationManager.registerSynchronization()} instead of this method.<a name="line.467"></a>
<span class="sourceLineNo">468</span>     * &lt;/p&gt;<a name="line.468"></a>
<span class="sourceLineNo">469</span>     *<a name="line.469"></a>
<span class="sourceLineNo">470</span>     * @param callback callback to invoke<a name="line.470"></a>
<span class="sourceLineNo">471</span>     * @throws IllegalArgumentException if {@code callback} is null<a name="line.471"></a>
<span class="sourceLineNo">472</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.472"></a>
<span class="sourceLineNo">473</span>     */<a name="line.473"></a>
<span class="sourceLineNo">474</span>    public synchronized void addCallback(Callback callback) {<a name="line.474"></a>
<span class="sourceLineNo">475</span>        if (callback == null)<a name="line.475"></a>
<span class="sourceLineNo">476</span>            throw new IllegalArgumentException("nullcallback ");<a name="line.476"></a>
<span class="sourceLineNo">477</span>        if (this.stale)<a name="line.477"></a>
<span class="sourceLineNo">478</span>            throw new StaleTransactionException(this);<a name="line.478"></a>
<span class="sourceLineNo">479</span>        this.callbacks.add(callback);<a name="line.479"></a>
<span class="sourceLineNo">480</span>    }<a name="line.480"></a>
<span class="sourceLineNo">481</span><a name="line.481"></a>
<span class="sourceLineNo">482</span>    /**<a name="line.482"></a>
<span class="sourceLineNo">483</span>     * Create an empty, in-memory transaction initialized with the same schema history as this transaction.<a name="line.483"></a>
<span class="sourceLineNo">484</span>     * The result can be used as a destination for "snapshot" copies of objects made via {@link #copy copy()}.<a name="line.484"></a>
<span class="sourceLineNo">485</span>     *<a name="line.485"></a>
<span class="sourceLineNo">486</span>     * &lt;p&gt;<a name="line.486"></a>
<span class="sourceLineNo">487</span>     * The returned {@link SnapshotTransaction} does not support {@link #commit}, {@link #rollback}, {@link #setRollbackOnly},<a name="line.487"></a>
<span class="sourceLineNo">488</span>     * or {@link #addCallback addCallback()}. However, it can be used indefinitely after this transaction closes.<a name="line.488"></a>
<span class="sourceLineNo">489</span>     * &lt;/p&gt;<a name="line.489"></a>
<span class="sourceLineNo">490</span>     *<a name="line.490"></a>
<span class="sourceLineNo">491</span>     * @return uncloseable, empty, in-memory transaction with compatible schema information<a name="line.491"></a>
<span class="sourceLineNo">492</span>     */<a name="line.492"></a>
<span class="sourceLineNo">493</span>    public SnapshotTransaction createSnapshotTransaction() {<a name="line.493"></a>
<span class="sourceLineNo">494</span>        return new SnapshotTransaction(this);<a name="line.494"></a>
<span class="sourceLineNo">495</span>    }<a name="line.495"></a>
<span class="sourceLineNo">496</span><a name="line.496"></a>
<span class="sourceLineNo">497</span>// Object Lifecycle<a name="line.497"></a>
<span class="sourceLineNo">498</span><a name="line.498"></a>
<span class="sourceLineNo">499</span>    /**<a name="line.499"></a>
<span class="sourceLineNo">500</span>     * Create a new object with the given object ID, if it doesn't already exist. If it does exist, nothing happens.<a name="line.500"></a>
<span class="sourceLineNo">501</span>     *<a name="line.501"></a>
<span class="sourceLineNo">502</span>     * &lt;p&gt;<a name="line.502"></a>
<span class="sourceLineNo">503</span>     * If the object doesn't already exist, all fields are set to their default values and the object's<a name="line.503"></a>
<span class="sourceLineNo">504</span>     * schema version is set to {@linkplain #getSchema() the version associated with this transaction}.<a name="line.504"></a>
<span class="sourceLineNo">505</span>     * &lt;/p&gt;<a name="line.505"></a>
<span class="sourceLineNo">506</span>     *<a name="line.506"></a>
<span class="sourceLineNo">507</span>     * @param id object ID<a name="line.507"></a>
<span class="sourceLineNo">508</span>     * @return true if the object did not exist and was created, false if the object already existed<a name="line.508"></a>
<span class="sourceLineNo">509</span>     * @throws UnknownTypeException if {@code id} does not correspond to a known object type in this transaction's schema version<a name="line.509"></a>
<span class="sourceLineNo">510</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.510"></a>
<span class="sourceLineNo">511</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.511"></a>
<span class="sourceLineNo">512</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.512"></a>
<span class="sourceLineNo">513</span>     */<a name="line.513"></a>
<span class="sourceLineNo">514</span>    public boolean create(ObjId id) {<a name="line.514"></a>
<span class="sourceLineNo">515</span>        return this.create(id, this.schema.versionNumber);<a name="line.515"></a>
<span class="sourceLineNo">516</span>    }<a name="line.516"></a>
<span class="sourceLineNo">517</span><a name="line.517"></a>
<span class="sourceLineNo">518</span>    /**<a name="line.518"></a>
<span class="sourceLineNo">519</span>     * Create a new object with the given object ID, if it doesn't already exist. If it does exist, nothing happens.<a name="line.519"></a>
<span class="sourceLineNo">520</span>     *<a name="line.520"></a>
<span class="sourceLineNo">521</span>     * &lt;p&gt;<a name="line.521"></a>
<span class="sourceLineNo">522</span>     * If the object doesn't already exist, all fields are set to their default values and the object's<a name="line.522"></a>
<span class="sourceLineNo">523</span>     * schema version is set to the specified version.<a name="line.523"></a>
<span class="sourceLineNo">524</span>     * &lt;/p&gt;<a name="line.524"></a>
<span class="sourceLineNo">525</span>     *<a name="line.525"></a>
<span class="sourceLineNo">526</span>     * @param id object ID<a name="line.526"></a>
<span class="sourceLineNo">527</span>     * @param versionNumber schema version number to use for newly created object<a name="line.527"></a>
<span class="sourceLineNo">528</span>     * @return true if the object did not exist and was created, false if the object already existed<a name="line.528"></a>
<span class="sourceLineNo">529</span>     * @throws UnknownTypeException if {@code id} does not correspond to a known object type in the specified schema version<a name="line.529"></a>
<span class="sourceLineNo">530</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.530"></a>
<span class="sourceLineNo">531</span>     * @throws IllegalArgumentException if {@code versionNumber} is invalid or unknown<a name="line.531"></a>
<span class="sourceLineNo">532</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.532"></a>
<span class="sourceLineNo">533</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.533"></a>
<span class="sourceLineNo">534</span>     */<a name="line.534"></a>
<span class="sourceLineNo">535</span>    public synchronized boolean create(ObjId id, int versionNumber) {<a name="line.535"></a>
<span class="sourceLineNo">536</span><a name="line.536"></a>
<span class="sourceLineNo">537</span>        // Sanity check<a name="line.537"></a>
<span class="sourceLineNo">538</span>        if (id == null)<a name="line.538"></a>
<span class="sourceLineNo">539</span>            throw new IllegalArgumentException("null id");<a name="line.539"></a>
<span class="sourceLineNo">540</span>        if (this.stale)<a name="line.540"></a>
<span class="sourceLineNo">541</span>            throw new StaleTransactionException(this);<a name="line.541"></a>
<span class="sourceLineNo">542</span><a name="line.542"></a>
<span class="sourceLineNo">543</span>        // Does object already exist?<a name="line.543"></a>
<span class="sourceLineNo">544</span>        if (this.exists(id))<a name="line.544"></a>
<span class="sourceLineNo">545</span>            return false;<a name="line.545"></a>
<span class="sourceLineNo">546</span><a name="line.546"></a>
<span class="sourceLineNo">547</span>        // Initialize object<a name="line.547"></a>
<span class="sourceLineNo">548</span>        this.initialize(id, this.schemas.getVersion(versionNumber).getObjType(id.getStorageId()));<a name="line.548"></a>
<span class="sourceLineNo">549</span><a name="line.549"></a>
<span class="sourceLineNo">550</span>        // Done<a name="line.550"></a>
<span class="sourceLineNo">551</span>        return true;<a name="line.551"></a>
<span class="sourceLineNo">552</span>    }<a name="line.552"></a>
<span class="sourceLineNo">553</span><a name="line.553"></a>
<span class="sourceLineNo">554</span>    /**<a name="line.554"></a>
<span class="sourceLineNo">555</span>     * Create a new object with a randomly assigned object ID and having the given type.<a name="line.555"></a>
<span class="sourceLineNo">556</span>     *<a name="line.556"></a>
<span class="sourceLineNo">557</span>     * &lt;p&gt;<a name="line.557"></a>
<span class="sourceLineNo">558</span>     * All fields will be set to their default values.<a name="line.558"></a>
<span class="sourceLineNo">559</span>     * The object's schema version will be set to {@linkplain #getSchema() the version associated with this transaction}.<a name="line.559"></a>
<span class="sourceLineNo">560</span>     * &lt;/p&gt;<a name="line.560"></a>
<span class="sourceLineNo">561</span>     *<a name="line.561"></a>
<span class="sourceLineNo">562</span>     * @param storageId object type storage ID<a name="line.562"></a>
<span class="sourceLineNo">563</span>     * @return object id of newly created object<a name="line.563"></a>
<span class="sourceLineNo">564</span>     * @throws UnknownTypeException if {@code storageId} does not correspond to a known object type in this transaction's schema<a name="line.564"></a>
<span class="sourceLineNo">565</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.565"></a>
<span class="sourceLineNo">566</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.566"></a>
<span class="sourceLineNo">567</span>     */<a name="line.567"></a>
<span class="sourceLineNo">568</span>    public ObjId create(int storageId) {<a name="line.568"></a>
<span class="sourceLineNo">569</span>        return this.create(storageId, this.schema.versionNumber);<a name="line.569"></a>
<span class="sourceLineNo">570</span>    }<a name="line.570"></a>
<span class="sourceLineNo">571</span><a name="line.571"></a>
<span class="sourceLineNo">572</span>    /**<a name="line.572"></a>
<span class="sourceLineNo">573</span>     * Create a new object with a randomly assigned object ID and having the given type and schema version.<a name="line.573"></a>
<span class="sourceLineNo">574</span>     *<a name="line.574"></a>
<span class="sourceLineNo">575</span>     * &lt;p&gt;<a name="line.575"></a>
<span class="sourceLineNo">576</span>     * All fields will be set to their default values.<a name="line.576"></a>
<span class="sourceLineNo">577</span>     * The object's schema version will be set to the specified version.<a name="line.577"></a>
<span class="sourceLineNo">578</span>     * &lt;/p&gt;<a name="line.578"></a>
<span class="sourceLineNo">579</span>     *<a name="line.579"></a>
<span class="sourceLineNo">580</span>     * @param storageId object type storage ID<a name="line.580"></a>
<span class="sourceLineNo">581</span>     * @param versionNumber schema version number to use for newly created object<a name="line.581"></a>
<span class="sourceLineNo">582</span>     * @return object id of newly created object<a name="line.582"></a>
<span class="sourceLineNo">583</span>     * @throws UnknownTypeException if {@code storageId} does not correspond to a known object type in the specified schema version<a name="line.583"></a>
<span class="sourceLineNo">584</span>     * @throws IllegalArgumentException if {@code versionNumber} is invalid or unknown<a name="line.584"></a>
<span class="sourceLineNo">585</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.585"></a>
<span class="sourceLineNo">586</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.586"></a>
<span class="sourceLineNo">587</span>     */<a name="line.587"></a>
<span class="sourceLineNo">588</span>    public synchronized ObjId create(int storageId, int versionNumber) {<a name="line.588"></a>
<span class="sourceLineNo">589</span><a name="line.589"></a>
<span class="sourceLineNo">590</span>        // Sanity check<a name="line.590"></a>
<span class="sourceLineNo">591</span>        if (this.stale)<a name="line.591"></a>
<span class="sourceLineNo">592</span>            throw new StaleTransactionException(this);<a name="line.592"></a>
<span class="sourceLineNo">593</span>        final ObjType objType = this.schemas.getVersion(versionNumber).getObjType(storageId);<a name="line.593"></a>
<span class="sourceLineNo">594</span><a name="line.594"></a>
<span class="sourceLineNo">595</span>        // Generate object ID<a name="line.595"></a>
<span class="sourceLineNo">596</span>        final ObjId id = this.generateIdValidated(objType.storageId);<a name="line.596"></a>
<span class="sourceLineNo">597</span><a name="line.597"></a>
<span class="sourceLineNo">598</span>        // Initialize object<a name="line.598"></a>
<span class="sourceLineNo">599</span>        this.initialize(id, objType);<a name="line.599"></a>
<span class="sourceLineNo">600</span><a name="line.600"></a>
<span class="sourceLineNo">601</span>        // Done<a name="line.601"></a>
<span class="sourceLineNo">602</span>        return id;<a name="line.602"></a>
<span class="sourceLineNo">603</span>    }<a name="line.603"></a>
<span class="sourceLineNo">604</span><a name="line.604"></a>
<span class="sourceLineNo">605</span>    /**<a name="line.605"></a>
<span class="sourceLineNo">606</span>     * Generate a random, unused {@link ObjId} for the given storage ID.<a name="line.606"></a>
<span class="sourceLineNo">607</span>     *<a name="line.607"></a>
<span class="sourceLineNo">608</span>     * @param storageId object type storage ID<a name="line.608"></a>
<span class="sourceLineNo">609</span>     * @return random unassigned object id<a name="line.609"></a>
<span class="sourceLineNo">610</span>     * @throws UnknownTypeException if {@code storageId} does not correspond to any known object type<a name="line.610"></a>
<span class="sourceLineNo">611</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.611"></a>
<span class="sourceLineNo">612</span>     */<a name="line.612"></a>
<span class="sourceLineNo">613</span>    public synchronized ObjId generateId(int storageId) {<a name="line.613"></a>
<span class="sourceLineNo">614</span><a name="line.614"></a>
<span class="sourceLineNo">615</span>        // Sanity check<a name="line.615"></a>
<span class="sourceLineNo">616</span>        if (this.stale)<a name="line.616"></a>
<span class="sourceLineNo">617</span>            throw new StaleTransactionException(this);<a name="line.617"></a>
<span class="sourceLineNo">618</span>        final ObjTypeStorageInfo info = this.schemas.verifyStorageInfo(storageId, ObjTypeStorageInfo.class);<a name="line.618"></a>
<span class="sourceLineNo">619</span><a name="line.619"></a>
<span class="sourceLineNo">620</span>        // Generate ID<a name="line.620"></a>
<span class="sourceLineNo">621</span>        return this.generateIdValidated(info.storageId);<a name="line.621"></a>
<span class="sourceLineNo">622</span>    }<a name="line.622"></a>
<span class="sourceLineNo">623</span><a name="line.623"></a>
<span class="sourceLineNo">624</span>    private /*synchronized*/ ObjId generateIdValidated(int storageId) {<a name="line.624"></a>
<span class="sourceLineNo">625</span><a name="line.625"></a>
<span class="sourceLineNo">626</span>        // Create a new, unique key<a name="line.626"></a>
<span class="sourceLineNo">627</span>        final ByteWriter keyWriter = new ByteWriter();<a name="line.627"></a>
<span class="sourceLineNo">628</span>        for (int attempts = 0; attempts &lt; MAX_GENERATED_KEY_ATTEMPTS; attempts++) {<a name="line.628"></a>
<span class="sourceLineNo">629</span>            final ObjId id = new ObjId(storageId);<a name="line.629"></a>
<span class="sourceLineNo">630</span>            id.writeTo(keyWriter);<a name="line.630"></a>
<span class="sourceLineNo">631</span>            if (this.kvt.get(keyWriter.getBytes()) == null)<a name="line.631"></a>
<span class="sourceLineNo">632</span>                return id;<a name="line.632"></a>
<span class="sourceLineNo">633</span>            keyWriter.reset(0);<a name="line.633"></a>
<span class="sourceLineNo">634</span>        }<a name="line.634"></a>
<span class="sourceLineNo">635</span><a name="line.635"></a>
<span class="sourceLineNo">636</span>        // Give up<a name="line.636"></a>
<span class="sourceLineNo">637</span>        throw new DatabaseException("could not find a new, unused object ID after "<a name="line.637"></a>
<span class="sourceLineNo">638</span>          + MAX_GENERATED_KEY_ATTEMPTS + " attempts; is our source of randomness truly random?");<a name="line.638"></a>
<span class="sourceLineNo">639</span>    }<a name="line.639"></a>
<span class="sourceLineNo">640</span><a name="line.640"></a>
<span class="sourceLineNo">641</span>    private synchronized void initialize(ObjId id, ObjType objType) {<a name="line.641"></a>
<span class="sourceLineNo">642</span><a name="line.642"></a>
<span class="sourceLineNo">643</span>        // Sanity check<a name="line.643"></a>
<span class="sourceLineNo">644</span>        if (this.stale)<a name="line.644"></a>
<span class="sourceLineNo">645</span>            throw new StaleTransactionException(this);<a name="line.645"></a>
<span class="sourceLineNo">646</span>        if (this.readOnly)<a name="line.646"></a>
<span class="sourceLineNo">647</span>            throw new ReadOnlyTransactionException(this);<a name="line.647"></a>
<span class="sourceLineNo">648</span><a name="line.648"></a>
<span class="sourceLineNo">649</span>        // Write object meta-data<a name="line.649"></a>
<span class="sourceLineNo">650</span>        ObjInfo.write(this, id, objType.schema.versionNumber, false);<a name="line.650"></a>
<span class="sourceLineNo">651</span><a name="line.651"></a>
<span class="sourceLineNo">652</span>        // Write object version index entry<a name="line.652"></a>
<span class="sourceLineNo">653</span>        this.kvt.put(Database.buildVersionIndexKey(id, objType.schema.versionNumber), ByteUtil.EMPTY);<a name="line.653"></a>
<span class="sourceLineNo">654</span><a name="line.654"></a>
<span class="sourceLineNo">655</span>        // Initialize counters to zero<a name="line.655"></a>
<span class="sourceLineNo">656</span>        if (!objType.counterFields.isEmpty()) {<a name="line.656"></a>
<span class="sourceLineNo">657</span>            for (CounterField field : objType.counterFields.values())<a name="line.657"></a>
<span class="sourceLineNo">658</span>                this.kvt.put(field.buildKey(id), this.kvt.encodeCounter(0));<a name="line.658"></a>
<span class="sourceLineNo">659</span>        }<a name="line.659"></a>
<span class="sourceLineNo">660</span><a name="line.660"></a>
<span class="sourceLineNo">661</span>        // Write simple field index entries<a name="line.661"></a>
<span class="sourceLineNo">662</span>        for (SimpleField&lt;?&gt; field : objType.simpleFields.values()) {<a name="line.662"></a>
<span class="sourceLineNo">663</span>            if (field.indexed)<a name="line.663"></a>
<span class="sourceLineNo">664</span>                this.kvt.put(Transaction.buildSimpleIndexEntry(field, id, null), ByteUtil.EMPTY);<a name="line.664"></a>
<span class="sourceLineNo">665</span>        }<a name="line.665"></a>
<span class="sourceLineNo">666</span><a name="line.666"></a>
<span class="sourceLineNo">667</span>        // Write composite index entries<a name="line.667"></a>
<span class="sourceLineNo">668</span>        for (CompositeIndex index : objType.compositeIndexes.values())<a name="line.668"></a>
<span class="sourceLineNo">669</span>            this.kvt.put(Transaction.buildDefaultCompositeIndexEntry(id, index), ByteUtil.EMPTY);<a name="line.669"></a>
<span class="sourceLineNo">670</span><a name="line.670"></a>
<span class="sourceLineNo">671</span>        // Notify listeners<a name="line.671"></a>
<span class="sourceLineNo">672</span>        for (CreateListener listener : this.createListeners.toArray(new CreateListener[this.createListeners.size()]))<a name="line.672"></a>
<span class="sourceLineNo">673</span>            listener.onCreate(this, id);<a name="line.673"></a>
<span class="sourceLineNo">674</span>    }<a name="line.674"></a>
<span class="sourceLineNo">675</span><a name="line.675"></a>
<span class="sourceLineNo">676</span>    /**<a name="line.676"></a>
<span class="sourceLineNo">677</span>     * Delete an object. Does nothing if object does not exist (e.g., has already been deleted).<a name="line.677"></a>
<span class="sourceLineNo">678</span>     *<a name="line.678"></a>
<span class="sourceLineNo">679</span>     * &lt;p&gt;<a name="line.679"></a>
<span class="sourceLineNo">680</span>     * This method does &lt;i&gt;not&lt;/i&gt; change the object's schema version if it has a different version that this transaction.<a name="line.680"></a>
<span class="sourceLineNo">681</span>     * &lt;/p&gt;<a name="line.681"></a>
<span class="sourceLineNo">682</span>     *<a name="line.682"></a>
<span class="sourceLineNo">683</span>     * &lt;p&gt;&lt;b&gt;Secondary Deletions&lt;/b&gt;&lt;/p&gt;<a name="line.683"></a>
<span class="sourceLineNo">684</span>     *<a name="line.684"></a>
<span class="sourceLineNo">685</span>     * &lt;p&gt;<a name="line.685"></a>
<span class="sourceLineNo">686</span>     * Deleting an object can trigger additional secondary deletions. Specifically,<a name="line.686"></a>
<span class="sourceLineNo">687</span>     * (a) if the object contains reference fields with {@linkplain ReferenceField#cascadeDelete delete cascade} enabled,<a name="line.687"></a>
<span class="sourceLineNo">688</span>     * any objects referred to through those fields will also be deleted, and (b) if the object is referred to by any other objects<a name="line.688"></a>
<span class="sourceLineNo">689</span>     * through fields configured for {@link DeleteAction#DELETE}, those referring objects will be deleted.<a name="line.689"></a>
<span class="sourceLineNo">690</span>     * &lt;/p&gt;<a name="line.690"></a>
<span class="sourceLineNo">691</span>     *<a name="line.691"></a>
<span class="sourceLineNo">692</span>     * &lt;p&gt;<a name="line.692"></a>
<span class="sourceLineNo">693</span>     * In any case, deletions occur one at a time, and only when an object is actually deleted are any associated secondary<a name="line.693"></a>
<span class="sourceLineNo">694</span>     * deletions added to an internal deletion queue. However, the order in which objects on this deletion queue are<a name="line.694"></a>
<span class="sourceLineNo">695</span>     * processed is unspecified. For an example of where this ordering matters, consider an object {@code A} referring to objects<a name="line.695"></a>
<span class="sourceLineNo">696</span>     * {@code B} and {@code C} with delete cascading references, where B also refers to C with a {@link DeleteAction#EXCEPTION}<a name="line.696"></a>
<span class="sourceLineNo">697</span>     * reference. Then if {@code A} is deleted, it's indeterminate whether a {@link ReferencedObjectException} will be thrown,<a name="line.697"></a>
<span class="sourceLineNo">698</span>     * as that depends on whether {@code B} or {@code C} is deleted first (with the answer being, respectively, no and yes).<a name="line.698"></a>
<span class="sourceLineNo">699</span>     * &lt;/p&gt;<a name="line.699"></a>
<span class="sourceLineNo">700</span>     *<a name="line.700"></a>
<span class="sourceLineNo">701</span>     * @param id object ID of the object to delete<a name="line.701"></a>
<span class="sourceLineNo">702</span>     * @return true if object was found and deleted, false if object was not found,<a name="line.702"></a>
<span class="sourceLineNo">703</span>     *  or if {@code id} specifies an unknown object type<a name="line.703"></a>
<span class="sourceLineNo">704</span>     * @throws ReferencedObjectException if the object is referenced by some other object<a name="line.704"></a>
<span class="sourceLineNo">705</span>     *  through a reference field configured for {@link DeleteAction#EXCEPTION}<a name="line.705"></a>
<span class="sourceLineNo">706</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.706"></a>
<span class="sourceLineNo">707</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.707"></a>
<span class="sourceLineNo">708</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.708"></a>
<span class="sourceLineNo">709</span>     */<a name="line.709"></a>
<span class="sourceLineNo">710</span>    public synchronized boolean delete(ObjId id) {<a name="line.710"></a>
<span class="sourceLineNo">711</span><a name="line.711"></a>
<span class="sourceLineNo">712</span>        // Sanity check<a name="line.712"></a>
<span class="sourceLineNo">713</span>        if (id == null)<a name="line.713"></a>
<span class="sourceLineNo">714</span>            throw new IllegalArgumentException("null id");<a name="line.714"></a>
<span class="sourceLineNo">715</span>        if (this.stale)<a name="line.715"></a>
<span class="sourceLineNo">716</span>            throw new StaleTransactionException(this);<a name="line.716"></a>
<span class="sourceLineNo">717</span>        if (this.readOnly)<a name="line.717"></a>
<span class="sourceLineNo">718</span>            throw new ReadOnlyTransactionException(this);<a name="line.718"></a>
<span class="sourceLineNo">719</span><a name="line.719"></a>
<span class="sourceLineNo">720</span>        // Get object info<a name="line.720"></a>
<span class="sourceLineNo">721</span>        try {<a name="line.721"></a>
<span class="sourceLineNo">722</span>            this.getObjectInfo(id, false);<a name="line.722"></a>
<span class="sourceLineNo">723</span>        } catch (DeletedObjectException | UnknownTypeException e) {<a name="line.723"></a>
<span class="sourceLineNo">724</span>            return false;<a name="line.724"></a>
<span class="sourceLineNo">725</span>        }<a name="line.725"></a>
<span class="sourceLineNo">726</span><a name="line.726"></a>
<span class="sourceLineNo">727</span>        // Handle delete cascade and recurive DeleteAction.DELETE without hogging Java stack<a name="line.727"></a>
<span class="sourceLineNo">728</span>        final ObjIdSet deletables = new ObjIdSet();<a name="line.728"></a>
<span class="sourceLineNo">729</span>        deletables.add(id);<a name="line.729"></a>
<span class="sourceLineNo">730</span>        boolean found = false;<a name="line.730"></a>
<span class="sourceLineNo">731</span>        while (!deletables.isEmpty())<a name="line.731"></a>
<span class="sourceLineNo">732</span>            found |= this.doDelete(deletables.iterator().next(), deletables);<a name="line.732"></a>
<span class="sourceLineNo">733</span><a name="line.733"></a>
<span class="sourceLineNo">734</span>        // Done<a name="line.734"></a>
<span class="sourceLineNo">735</span>        return found;<a name="line.735"></a>
<span class="sourceLineNo">736</span>    }<a name="line.736"></a>
<span class="sourceLineNo">737</span><a name="line.737"></a>
<span class="sourceLineNo">738</span>    private synchronized boolean doDelete(final ObjId id, ObjIdSet deletables) {<a name="line.738"></a>
<span class="sourceLineNo">739</span><a name="line.739"></a>
<span class="sourceLineNo">740</span>        // Loop here to handle any mutations within delete notification listener callbacks<a name="line.740"></a>
<span class="sourceLineNo">741</span>        ObjInfo info;<a name="line.741"></a>
<span class="sourceLineNo">742</span>        while (true) {<a name="line.742"></a>
<span class="sourceLineNo">743</span><a name="line.743"></a>
<span class="sourceLineNo">744</span>            // Get object info<a name="line.744"></a>
<span class="sourceLineNo">745</span>            try {<a name="line.745"></a>
<span class="sourceLineNo">746</span>                info = this.getObjectInfo(id, false);<a name="line.746"></a>
<span class="sourceLineNo">747</span>            } catch (DeletedObjectException e) {                    // possibly due to a cycle of DeleteAction.DELETE references<a name="line.747"></a>
<span class="sourceLineNo">748</span>                deletables.remove(id);<a name="line.748"></a>
<span class="sourceLineNo">749</span>                return false;<a name="line.749"></a>
<span class="sourceLineNo">750</span>            } catch (UnknownTypeException e) {<a name="line.750"></a>
<span class="sourceLineNo">751</span>                throw new InconsistentDatabaseException("encountered reference with unknown type during delete cascade: " + id, e);<a name="line.751"></a>
<span class="sourceLineNo">752</span>            }<a name="line.752"></a>
<span class="sourceLineNo">753</span><a name="line.753"></a>
<span class="sourceLineNo">754</span>            // Determine if any EXCEPTION reference fields refer to the object (from some other object); if so, throw exception<a name="line.754"></a>
<span class="sourceLineNo">755</span>            for (ReferenceFieldStorageInfo fieldInfo :<a name="line.755"></a>
<span class="sourceLineNo">756</span>              Iterables.filter(this.schemas.storageInfos.values(), ReferenceFieldStorageInfo.class)) {<a name="line.756"></a>
<span class="sourceLineNo">757</span>                for (ObjId referrer : this.findReferrers(id, DeleteAction.EXCEPTION, fieldInfo.storageId)) {<a name="line.757"></a>
<span class="sourceLineNo">758</span>                    if (!referrer.equals(id))<a name="line.758"></a>
<span class="sourceLineNo">759</span>                        throw new ReferencedObjectException(id, referrer, fieldInfo.storageId);<a name="line.759"></a>
<span class="sourceLineNo">760</span>                }<a name="line.760"></a>
<span class="sourceLineNo">761</span>            }<a name="line.761"></a>
<span class="sourceLineNo">762</span><a name="line.762"></a>
<span class="sourceLineNo">763</span>            // Do we need to issue delete notifications for the object being deleted?<a name="line.763"></a>
<span class="sourceLineNo">764</span>            if (info.isDeleteNotified() || this.deleteListeners.isEmpty())<a name="line.764"></a>
<span class="sourceLineNo">765</span>                break;<a name="line.765"></a>
<span class="sourceLineNo">766</span><a name="line.766"></a>
<span class="sourceLineNo">767</span>            // Issue delete notifications and retry<a name="line.767"></a>
<span class="sourceLineNo">768</span>            ObjInfo.write(this, id, info.getVersion(), true);<a name="line.768"></a>
<span class="sourceLineNo">769</span>            for (DeleteListener listener : this.deleteListeners.toArray(new DeleteListener[this.deleteListeners.size()]))<a name="line.769"></a>
<span class="sourceLineNo">770</span>                listener.onDelete(this, id);<a name="line.770"></a>
<span class="sourceLineNo">771</span>        }<a name="line.771"></a>
<span class="sourceLineNo">772</span><a name="line.772"></a>
<span class="sourceLineNo">773</span>        // Find all objects referred to by a reference field with cascadeDelete = true and add them to deletables<a name="line.773"></a>
<span class="sourceLineNo">774</span>        for (ReferenceField field : Iterables.filter(<a name="line.774"></a>
<span class="sourceLineNo">775</span>          Iterables.filter(info.getObjType().referenceFields.values(), ReferenceField.class), new HasCascadeDelete())) {<a name="line.775"></a>
<span class="sourceLineNo">776</span>            final Iterable&lt;ObjId&gt; refs = field.parent != null ?<a name="line.776"></a>
<span class="sourceLineNo">777</span>              field.parent.iterateSubField(this, id, field) : Collections.singleton(field.getValue(this, id));<a name="line.777"></a>
<span class="sourceLineNo">778</span>            for (ObjId ref : refs) {<a name="line.778"></a>
<span class="sourceLineNo">779</span>                if (ref != null)<a name="line.779"></a>
<span class="sourceLineNo">780</span>                    deletables.add(ref);<a name="line.780"></a>
<span class="sourceLineNo">781</span>            }<a name="line.781"></a>
<span class="sourceLineNo">782</span>        }<a name="line.782"></a>
<span class="sourceLineNo">783</span><a name="line.783"></a>
<span class="sourceLineNo">784</span>        // Actually delete the object<a name="line.784"></a>
<span class="sourceLineNo">785</span>        this.deleteObjectData(info);<a name="line.785"></a>
<span class="sourceLineNo">786</span>        deletables.remove(id);<a name="line.786"></a>
<span class="sourceLineNo">787</span><a name="line.787"></a>
<span class="sourceLineNo">788</span>        // Find all UNREFERENCE references and unreference them<a name="line.788"></a>
<span class="sourceLineNo">789</span>        for (ReferenceFieldStorageInfo fieldInfo :<a name="line.789"></a>
<span class="sourceLineNo">790</span>          Iterables.filter(this.schemas.storageInfos.values(), ReferenceFieldStorageInfo.class)) {<a name="line.790"></a>
<span class="sourceLineNo">791</span>            final NavigableSet&lt;ObjId&gt; referrers = this.findReferrers(id, DeleteAction.UNREFERENCE, fieldInfo.storageId);<a name="line.791"></a>
<span class="sourceLineNo">792</span>            if (fieldInfo.isSubField()) {<a name="line.792"></a>
<span class="sourceLineNo">793</span>                final ComplexFieldStorageInfo&lt;?&gt; superFieldInfo = this.schemas.verifyStorageInfo(<a name="line.793"></a>
<span class="sourceLineNo">794</span>                  fieldInfo.superFieldStorageId, ComplexFieldStorageInfo.class);<a name="line.794"></a>
<span class="sourceLineNo">795</span>                superFieldInfo.unreferenceAll(this, fieldInfo.storageId, id, referrers);<a name="line.795"></a>
<span class="sourceLineNo">796</span>            } else {<a name="line.796"></a>
<span class="sourceLineNo">797</span>                final int storageId = fieldInfo.storageId;<a name="line.797"></a>
<span class="sourceLineNo">798</span>                for (ObjId referrer : referrers)<a name="line.798"></a>
<span class="sourceLineNo">799</span>                    this.writeSimpleField(referrer, storageId, null, false);<a name="line.799"></a>
<span class="sourceLineNo">800</span>            }<a name="line.800"></a>
<span class="sourceLineNo">801</span>        }<a name="line.801"></a>
<span class="sourceLineNo">802</span><a name="line.802"></a>
<span class="sourceLineNo">803</span>        // Find all DELETE references and mark the containing object for deletion (caller will call us back to actually delete)<a name="line.803"></a>
<span class="sourceLineNo">804</span>        for (ObjId referrer : this.findReferrers(id, DeleteAction.DELETE, -1))<a name="line.804"></a>
<span class="sourceLineNo">805</span>            deletables.add(referrer);<a name="line.805"></a>
<span class="sourceLineNo">806</span><a name="line.806"></a>
<span class="sourceLineNo">807</span>        // Done<a name="line.807"></a>
<span class="sourceLineNo">808</span>        return true;<a name="line.808"></a>
<span class="sourceLineNo">809</span>    }<a name="line.809"></a>
<span class="sourceLineNo">810</span><a name="line.810"></a>
<span class="sourceLineNo">811</span>    // Delete all of an object's data<a name="line.811"></a>
<span class="sourceLineNo">812</span>    private void deleteObjectData(ObjInfo info) {<a name="line.812"></a>
<span class="sourceLineNo">813</span><a name="line.813"></a>
<span class="sourceLineNo">814</span>        // Delete object's simple field index entries<a name="line.814"></a>
<span class="sourceLineNo">815</span>        final ObjId id = info.getId();<a name="line.815"></a>
<span class="sourceLineNo">816</span>        final ObjType type = info.getObjType();<a name="line.816"></a>
<span class="sourceLineNo">817</span>        for (SimpleField&lt;?&gt; field : type.simpleFields.values()) {<a name="line.817"></a>
<span class="sourceLineNo">818</span>            if (field.indexed)<a name="line.818"></a>
<span class="sourceLineNo">819</span>                this.kvt.remove(Transaction.buildSimpleIndexEntry(field, id, this.kvt.get(field.buildKey(id))));<a name="line.819"></a>
<span class="sourceLineNo">820</span>        }<a name="line.820"></a>
<span class="sourceLineNo">821</span><a name="line.821"></a>
<span class="sourceLineNo">822</span>        // Delete object's composite index entries<a name="line.822"></a>
<span class="sourceLineNo">823</span>        for (CompositeIndex index : type.compositeIndexes.values())<a name="line.823"></a>
<span class="sourceLineNo">824</span>            this.kvt.remove(this.buildCompositeIndexEntry(id, index));<a name="line.824"></a>
<span class="sourceLineNo">825</span><a name="line.825"></a>
<span class="sourceLineNo">826</span>        // Delete object's complex field index entries<a name="line.826"></a>
<span class="sourceLineNo">827</span>        for (ComplexField&lt;?&gt; field : type.complexFields.values())<a name="line.827"></a>
<span class="sourceLineNo">828</span>            field.removeIndexEntries(this, id);<a name="line.828"></a>
<span class="sourceLineNo">829</span><a name="line.829"></a>
<span class="sourceLineNo">830</span>        // Delete object meta-data and all field content<a name="line.830"></a>
<span class="sourceLineNo">831</span>        final byte[] minKey = info.getId().getBytes();<a name="line.831"></a>
<span class="sourceLineNo">832</span>        final byte[] maxKey = ByteUtil.getKeyAfterPrefix(minKey);<a name="line.832"></a>
<span class="sourceLineNo">833</span>        this.kvt.removeRange(minKey, maxKey);<a name="line.833"></a>
<span class="sourceLineNo">834</span><a name="line.834"></a>
<span class="sourceLineNo">835</span>        // Delete object schema version entry<a name="line.835"></a>
<span class="sourceLineNo">836</span>        this.kvt.remove(Database.buildVersionIndexKey(id, info.getVersion()));<a name="line.836"></a>
<span class="sourceLineNo">837</span>    }<a name="line.837"></a>
<span class="sourceLineNo">838</span><a name="line.838"></a>
<span class="sourceLineNo">839</span>    /**<a name="line.839"></a>
<span class="sourceLineNo">840</span>     * Determine if an object exists.<a name="line.840"></a>
<span class="sourceLineNo">841</span>     *<a name="line.841"></a>
<span class="sourceLineNo">842</span>     * &lt;p&gt;<a name="line.842"></a>
<span class="sourceLineNo">843</span>     * This method does &lt;i&gt;not&lt;/i&gt; change the object's schema version if it exists<a name="line.843"></a>
<span class="sourceLineNo">844</span>     * and has a different version that this transaction.<a name="line.844"></a>
<span class="sourceLineNo">845</span>     * &lt;/p&gt;<a name="line.845"></a>
<span class="sourceLineNo">846</span>     *<a name="line.846"></a>
<span class="sourceLineNo">847</span>     * @param id object ID of the object to find<a name="line.847"></a>
<span class="sourceLineNo">848</span>     * @return true if object was found, false if object was not found, or if {@code id} specifies an unknown object type<a name="line.848"></a>
<span class="sourceLineNo">849</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.849"></a>
<span class="sourceLineNo">850</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.850"></a>
<span class="sourceLineNo">851</span>     */<a name="line.851"></a>
<span class="sourceLineNo">852</span>    public synchronized boolean exists(ObjId id) {<a name="line.852"></a>
<span class="sourceLineNo">853</span>        try {<a name="line.853"></a>
<span class="sourceLineNo">854</span>            this.getObjectInfo(id, false);<a name="line.854"></a>
<span class="sourceLineNo">855</span>        } catch (DeletedObjectException | UnknownTypeException e) {<a name="line.855"></a>
<span class="sourceLineNo">856</span>            return false;<a name="line.856"></a>
<span class="sourceLineNo">857</span>        }<a name="line.857"></a>
<span class="sourceLineNo">858</span>        return true;<a name="line.858"></a>
<span class="sourceLineNo">859</span>    }<a name="line.859"></a>
<span class="sourceLineNo">860</span><a name="line.860"></a>
<span class="sourceLineNo">861</span>    /**<a name="line.861"></a>
<span class="sourceLineNo">862</span>     * Copy all of an object's fields onto a target object in a (possibly) different transaction, replacing any previous values.<a name="line.862"></a>
<span class="sourceLineNo">863</span>     *<a name="line.863"></a>
<span class="sourceLineNo">864</span>     * &lt;p&gt;<a name="line.864"></a>
<span class="sourceLineNo">865</span>     * If necessary, {@code source} object is first upgraded to {@linkplain #getSchema() the schema version associated with<a name="line.865"></a>
<span class="sourceLineNo">866</span>     * this transaction}, which must be identical in this transaction and {@code dest}.<a name="line.866"></a>
<span class="sourceLineNo">867</span>     * &lt;/p&gt;<a name="line.867"></a>
<span class="sourceLineNo">868</span>     *<a name="line.868"></a>
<span class="sourceLineNo">869</span>     * &lt;p&gt;<a name="line.869"></a>
<span class="sourceLineNo">870</span>     * Only the object's fields are copied; any other objects they reference are not copied. If the {@code target} object<a name="line.870"></a>
<span class="sourceLineNo">871</span>     * does not exist in {@code dest}, it will be created first (and {@link CreateListener}s notified); if {@code target}<a name="line.871"></a>
<span class="sourceLineNo">872</span>     * does exist in {@code dest}, it's schema version will be upgraded if necessary to match {@code source} (and any registered<a name="line.872"></a>
<span class="sourceLineNo">873</span>     * {@link VersionChangeListener}s notified).<a name="line.873"></a>
<span class="sourceLineNo">874</span>     * (Meaningful) changes to {@code target}'s fields generate change listener notifications.<a name="line.874"></a>
<span class="sourceLineNo">875</span>     * &lt;/p&gt;<a name="line.875"></a>
<span class="sourceLineNo">876</span>     *<a name="line.876"></a>
<span class="sourceLineNo">877</span>     * &lt;p&gt;<a name="line.877"></a>
<span class="sourceLineNo">878</span>     * Note: if two threads attempt to copy objects between the same two transactions at the same time but in opposite directions,<a name="line.878"></a>
<span class="sourceLineNo">879</span>     * deadlock could result.<a name="line.879"></a>
<span class="sourceLineNo">880</span>     * &lt;/p&gt;<a name="line.880"></a>
<span class="sourceLineNo">881</span>     *<a name="line.881"></a>
<span class="sourceLineNo">882</span>     * &lt;p&gt;<a name="line.882"></a>
<span class="sourceLineNo">883</span>     * If {@code dest} is this instance, and {@code source} equals {@code target}, no changes are made and false is returned.<a name="line.883"></a>
<span class="sourceLineNo">884</span>     * &lt;/p&gt;<a name="line.884"></a>
<span class="sourceLineNo">885</span>     *<a name="line.885"></a>
<span class="sourceLineNo">886</span>     * @param source object ID of the source object in this transaction<a name="line.886"></a>
<span class="sourceLineNo">887</span>     * @param target object ID of the target object in {@code dest}<a name="line.887"></a>
<span class="sourceLineNo">888</span>     * @param dest destination transaction containing {@code target} (possibly same as this transaction)<a name="line.888"></a>
<span class="sourceLineNo">889</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.889"></a>
<span class="sourceLineNo">890</span>     * @return false if object already existed in {@code dest}, true if {@code target} did not exist in {@code dest}<a name="line.890"></a>
<span class="sourceLineNo">891</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found in this transaction<a name="line.891"></a>
<span class="sourceLineNo">892</span>     * @throws UnknownTypeException if {@code source} or {@code target} specifies an unknown object type<a name="line.892"></a>
<span class="sourceLineNo">893</span>     * @throws IllegalArgumentException if {@code source} and {@code target} specify different object types<a name="line.893"></a>
<span class="sourceLineNo">894</span>     * @throws IllegalArgumentException if any parameter is null<a name="line.894"></a>
<span class="sourceLineNo">895</span>     * @throws IllegalArgumentException if any {@code source} and {@code target} have different object types<a name="line.895"></a>
<span class="sourceLineNo">896</span>     * @throws ReadOnlyTransactionException if {@code dest} has been {@linkplain #setReadOnly set read-only}<a name="line.896"></a>
<span class="sourceLineNo">897</span>     * @throws StaleTransactionException if this transaction or {@code dest} is no longer usable<a name="line.897"></a>
<span class="sourceLineNo">898</span>     * @throws SchemaMismatchException if the schema version associated with {@code source} differs between<a name="line.898"></a>
<span class="sourceLineNo">899</span>     *  this transaction and {@code dest}<a name="line.899"></a>
<span class="sourceLineNo">900</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.900"></a>
<span class="sourceLineNo">901</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.901"></a>
<span class="sourceLineNo">902</span>     */<a name="line.902"></a>
<span class="sourceLineNo">903</span>    public synchronized boolean copy(ObjId source, final ObjId target, final Transaction dest, final boolean updateVersion) {<a name="line.903"></a>
<span class="sourceLineNo">904</span><a name="line.904"></a>
<span class="sourceLineNo">905</span>        // Sanity check<a name="line.905"></a>
<span class="sourceLineNo">906</span>        if (source == null)<a name="line.906"></a>
<span class="sourceLineNo">907</span>            throw new IllegalArgumentException("null source");<a name="line.907"></a>
<span class="sourceLineNo">908</span>        if (target == null)<a name="line.908"></a>
<span class="sourceLineNo">909</span>            throw new IllegalArgumentException("null target");<a name="line.909"></a>
<span class="sourceLineNo">910</span>        if (dest == null)<a name="line.910"></a>
<span class="sourceLineNo">911</span>            throw new IllegalArgumentException("null dest");<a name="line.911"></a>
<span class="sourceLineNo">912</span>        if (this.stale)<a name="line.912"></a>
<span class="sourceLineNo">913</span>            throw new StaleTransactionException(this);<a name="line.913"></a>
<span class="sourceLineNo">914</span><a name="line.914"></a>
<span class="sourceLineNo">915</span>        // Do nothing if nothing to do<a name="line.915"></a>
<span class="sourceLineNo">916</span>        if (this == dest &amp;&amp; source.equals(target))<a name="line.916"></a>
<span class="sourceLineNo">917</span>            return false;<a name="line.917"></a>
<span class="sourceLineNo">918</span><a name="line.918"></a>
<span class="sourceLineNo">919</span>        // Get object info<a name="line.919"></a>
<span class="sourceLineNo">920</span>        final ObjInfo srcInfo = this.getObjectInfo(source, updateVersion);<a name="line.920"></a>
<span class="sourceLineNo">921</span><a name="line.921"></a>
<span class="sourceLineNo">922</span>        // Do the copy while both transactions are locked<a name="line.922"></a>
<span class="sourceLineNo">923</span>        synchronized (dest) {<a name="line.923"></a>
<span class="sourceLineNo">924</span><a name="line.924"></a>
<span class="sourceLineNo">925</span>            // Sanity check<a name="line.925"></a>
<span class="sourceLineNo">926</span>            if (dest.stale)<a name="line.926"></a>
<span class="sourceLineNo">927</span>                throw new StaleTransactionException(dest);<a name="line.927"></a>
<span class="sourceLineNo">928</span>            if (dest.readOnly)<a name="line.928"></a>
<span class="sourceLineNo">929</span>                throw new ReadOnlyTransactionException(dest);<a name="line.929"></a>
<span class="sourceLineNo">930</span><a name="line.930"></a>
<span class="sourceLineNo">931</span>            // Copy fields<a name="line.931"></a>
<span class="sourceLineNo">932</span>            return dest.mutateAndNotify(new Mutation&lt;Boolean&gt;() {<a name="line.932"></a>
<span class="sourceLineNo">933</span>                @Override<a name="line.933"></a>
<span class="sourceLineNo">934</span>                public Boolean mutate() {<a name="line.934"></a>
<span class="sourceLineNo">935</span>                    return Transaction.doCopyFields(srcInfo, target, Transaction.this, dest, updateVersion);<a name="line.935"></a>
<span class="sourceLineNo">936</span>                }<a name="line.936"></a>
<span class="sourceLineNo">937</span>            });<a name="line.937"></a>
<span class="sourceLineNo">938</span>        }<a name="line.938"></a>
<span class="sourceLineNo">939</span>    }<a name="line.939"></a>
<span class="sourceLineNo">940</span><a name="line.940"></a>
<span class="sourceLineNo">941</span>    // This method assumes both transactions are locked<a name="line.941"></a>
<span class="sourceLineNo">942</span>    private static boolean doCopyFields(ObjInfo srcInfo, ObjId dstId, Transaction srcTx, Transaction dstTx, boolean updateVersion) {<a name="line.942"></a>
<span class="sourceLineNo">943</span><a name="line.943"></a>
<span class="sourceLineNo">944</span>        // Sanity check<a name="line.944"></a>
<span class="sourceLineNo">945</span>        final ObjId srcId = srcInfo.getId();<a name="line.945"></a>
<span class="sourceLineNo">946</span>        if (srcId.equals(dstId) &amp;&amp; srcTx == dstTx)<a name="line.946"></a>
<span class="sourceLineNo">947</span>            throw new RuntimeException("internal error");<a name="line.947"></a>
<span class="sourceLineNo">948</span><a name="line.948"></a>
<span class="sourceLineNo">949</span>        // Verify objects have the same type<a name="line.949"></a>
<span class="sourceLineNo">950</span>        if (srcId.getStorageId() != dstId.getStorageId()) {<a name="line.950"></a>
<span class="sourceLineNo">951</span>            throw new IllegalArgumentException("can't copy " + srcId + " to " + dstId<a name="line.951"></a>
<span class="sourceLineNo">952</span>              + " due to non-equal object types (" + srcId.getStorageId() + " != " + dstId.getStorageId() + ")");<a name="line.952"></a>
<span class="sourceLineNo">953</span>        }<a name="line.953"></a>
<span class="sourceLineNo">954</span><a name="line.954"></a>
<span class="sourceLineNo">955</span>        // Upgrade source object if necessary<a name="line.955"></a>
<span class="sourceLineNo">956</span>        if (updateVersion &amp;&amp; srcInfo.getVersion() != srcTx.schema.versionNumber) {<a name="line.956"></a>
<span class="sourceLineNo">957</span>            srcTx.updateVersion(srcInfo, srcTx.schema);<a name="line.957"></a>
<span class="sourceLineNo">958</span>            srcInfo = new ObjInfo(srcTx, srcId);<a name="line.958"></a>
<span class="sourceLineNo">959</span>        }<a name="line.959"></a>
<span class="sourceLineNo">960</span><a name="line.960"></a>
<span class="sourceLineNo">961</span>        // Find and verify source object's schema version in destination transaction<a name="line.961"></a>
<span class="sourceLineNo">962</span>        final int objectVersion = srcInfo.getVersion();<a name="line.962"></a>
<span class="sourceLineNo">963</span>        final Schema dstSchema;<a name="line.963"></a>
<span class="sourceLineNo">964</span>        try {<a name="line.964"></a>
<span class="sourceLineNo">965</span>            dstSchema = dstTx.schemas.getVersion(objectVersion);<a name="line.965"></a>
<span class="sourceLineNo">966</span>        } catch (IllegalArgumentException e) {<a name="line.966"></a>
<span class="sourceLineNo">967</span>            throw new SchemaMismatchException("destination transaction has no schema version " + objectVersion);<a name="line.967"></a>
<span class="sourceLineNo">968</span>        }<a name="line.968"></a>
<span class="sourceLineNo">969</span>        if (!Arrays.equals(srcTx.schema.encodedXML, dstTx.schema.encodedXML))<a name="line.969"></a>
<span class="sourceLineNo">970</span>            throw new SchemaMismatchException("destination transaction schema version " + objectVersion + " does not match");<a name="line.970"></a>
<span class="sourceLineNo">971</span><a name="line.971"></a>
<span class="sourceLineNo">972</span>        // Create destination object if it doesn't already exist<a name="line.972"></a>
<span class="sourceLineNo">973</span>        final boolean existed = !dstTx.create(dstId, objectVersion);<a name="line.973"></a>
<span class="sourceLineNo">974</span>        final ObjInfo dstInfo = dstTx.getObjectInfo(dstId, false);<a name="line.974"></a>
<span class="sourceLineNo">975</span>        final boolean needUpgrade = dstInfo.getVersion() != objectVersion;      // can only happen if object already existed<a name="line.975"></a>
<span class="sourceLineNo">976</span><a name="line.976"></a>
<span class="sourceLineNo">977</span>        // Do field-by-field copy if there are change or version listeners, otherwise do fast copy of key/value pairs<a name="line.977"></a>
<span class="sourceLineNo">978</span>        final ObjType type = srcInfo.getObjType();<a name="line.978"></a>
<span class="sourceLineNo">979</span>        if (dstTx.hasFieldMonitor(type) || (needUpgrade &amp;&amp; !dstTx.versionChangeListeners.isEmpty())) {<a name="line.979"></a>
<span class="sourceLineNo">980</span><a name="line.980"></a>
<span class="sourceLineNo">981</span>            // Upgrade object first<a name="line.981"></a>
<span class="sourceLineNo">982</span>            if (needUpgrade)<a name="line.982"></a>
<span class="sourceLineNo">983</span>                dstTx.updateVersion(dstInfo, dstSchema);<a name="line.983"></a>
<span class="sourceLineNo">984</span><a name="line.984"></a>
<span class="sourceLineNo">985</span>            // Copy fields<a name="line.985"></a>
<span class="sourceLineNo">986</span>            for (Field&lt;?&gt; field : type.fields.values())<a name="line.986"></a>
<span class="sourceLineNo">987</span>                field.copy(srcId, dstId, srcTx, dstTx);<a name="line.987"></a>
<span class="sourceLineNo">988</span>        } else {<a name="line.988"></a>
<span class="sourceLineNo">989</span><a name="line.989"></a>
<span class="sourceLineNo">990</span>            // Delete pre-existing object's field content, if any<a name="line.990"></a>
<span class="sourceLineNo">991</span>            dstTx.deleteObjectData(dstInfo);<a name="line.991"></a>
<span class="sourceLineNo">992</span><a name="line.992"></a>
<span class="sourceLineNo">993</span>            // Add schema version index entry<a name="line.993"></a>
<span class="sourceLineNo">994</span>            dstTx.kvt.put(Database.buildVersionIndexKey(dstId, objectVersion), ByteUtil.EMPTY);<a name="line.994"></a>
<span class="sourceLineNo">995</span><a name="line.995"></a>
<span class="sourceLineNo">996</span>            // Copy object meta-data and all field content in one key range sweep<a name="line.996"></a>
<span class="sourceLineNo">997</span>            final byte[] srcMinKey = srcId.getBytes();<a name="line.997"></a>
<span class="sourceLineNo">998</span>            final byte[] srcMaxKey = ByteUtil.getKeyAfterPrefix(srcMinKey);<a name="line.998"></a>
<span class="sourceLineNo">999</span>            final ByteWriter dstWriter = new ByteWriter();<a name="line.999"></a>
<span class="sourceLineNo">1000</span>            dstWriter.write(dstId.getBytes());<a name="line.1000"></a>
<span class="sourceLineNo">1001</span>            final int dstMark = dstWriter.mark();<a name="line.1001"></a>
<span class="sourceLineNo">1002</span>            for (Iterator&lt;KVPair&gt; i = srcTx.kvt.getRange(srcMinKey, srcMaxKey, false); i.hasNext(); ) {<a name="line.1002"></a>
<span class="sourceLineNo">1003</span>                final KVPair kv = i.next();<a name="line.1003"></a>
<span class="sourceLineNo">1004</span>                final ByteReader srcReader = new ByteReader(kv.getKey());<a name="line.1004"></a>
<span class="sourceLineNo">1005</span>                srcReader.skip(srcMinKey.length);<a name="line.1005"></a>
<span class="sourceLineNo">1006</span>                dstWriter.reset(dstMark);<a name="line.1006"></a>
<span class="sourceLineNo">1007</span>                dstWriter.write(srcReader);<a name="line.1007"></a>
<span class="sourceLineNo">1008</span>                dstTx.kvt.put(dstWriter.getBytes(), kv.getValue());<a name="line.1008"></a>
<span class="sourceLineNo">1009</span>            }<a name="line.1009"></a>
<span class="sourceLineNo">1010</span><a name="line.1010"></a>
<span class="sourceLineNo">1011</span>            // Create object's simple field index entries<a name="line.1011"></a>
<span class="sourceLineNo">1012</span>            for (SimpleField&lt;?&gt; field : type.simpleFields.values()) {<a name="line.1012"></a>
<span class="sourceLineNo">1013</span>                if (field.indexed) {<a name="line.1013"></a>
<span class="sourceLineNo">1014</span>                    final byte[] fieldValue = dstTx.kvt.get(field.buildKey(dstId));     // can be null (if field has default value)<a name="line.1014"></a>
<span class="sourceLineNo">1015</span>                    final byte[] indexKey = Transaction.buildSimpleIndexEntry(field, dstId, fieldValue);<a name="line.1015"></a>
<span class="sourceLineNo">1016</span>                    dstTx.kvt.put(indexKey, ByteUtil.EMPTY);<a name="line.1016"></a>
<span class="sourceLineNo">1017</span>                }<a name="line.1017"></a>
<span class="sourceLineNo">1018</span>            }<a name="line.1018"></a>
<span class="sourceLineNo">1019</span><a name="line.1019"></a>
<span class="sourceLineNo">1020</span>            // Create object's composite index entries<a name="line.1020"></a>
<span class="sourceLineNo">1021</span>            for (CompositeIndex index : type.compositeIndexes.values())<a name="line.1021"></a>
<span class="sourceLineNo">1022</span>                dstTx.kvt.put(Transaction.buildCompositeIndexEntry(dstTx, dstId, index), ByteUtil.EMPTY);<a name="line.1022"></a>
<span class="sourceLineNo">1023</span><a name="line.1023"></a>
<span class="sourceLineNo">1024</span>            // Create object's complex field index entries<a name="line.1024"></a>
<span class="sourceLineNo">1025</span>            for (ComplexField&lt;?&gt; field : type.complexFields.values()) {<a name="line.1025"></a>
<span class="sourceLineNo">1026</span>                for (SimpleField&lt;?&gt; subField : field.getSubFields()) {<a name="line.1026"></a>
<span class="sourceLineNo">1027</span>                    if (subField.indexed)<a name="line.1027"></a>
<span class="sourceLineNo">1028</span>                        field.addIndexEntries(dstTx, dstId, subField);<a name="line.1028"></a>
<span class="sourceLineNo">1029</span>                }<a name="line.1029"></a>
<span class="sourceLineNo">1030</span>            }<a name="line.1030"></a>
<span class="sourceLineNo">1031</span>        }<a name="line.1031"></a>
<span class="sourceLineNo">1032</span><a name="line.1032"></a>
<span class="sourceLineNo">1033</span>        // Done<a name="line.1033"></a>
<span class="sourceLineNo">1034</span>        return !existed;<a name="line.1034"></a>
<span class="sourceLineNo">1035</span>    }<a name="line.1035"></a>
<span class="sourceLineNo">1036</span><a name="line.1036"></a>
<span class="sourceLineNo">1037</span>    /**<a name="line.1037"></a>
<span class="sourceLineNo">1038</span>     * Add a {@link CreateListener} to this transaction.<a name="line.1038"></a>
<span class="sourceLineNo">1039</span>     *<a name="line.1039"></a>
<span class="sourceLineNo">1040</span>     * @param listener the listener to add<a name="line.1040"></a>
<span class="sourceLineNo">1041</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1041"></a>
<span class="sourceLineNo">1042</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1042"></a>
<span class="sourceLineNo">1043</span>     */<a name="line.1043"></a>
<span class="sourceLineNo">1044</span>    public synchronized void addCreateListener(CreateListener listener) {<a name="line.1044"></a>
<span class="sourceLineNo">1045</span>        if (this.stale)<a name="line.1045"></a>
<span class="sourceLineNo">1046</span>            throw new StaleTransactionException(this);<a name="line.1046"></a>
<span class="sourceLineNo">1047</span>        if (listener == null)<a name="line.1047"></a>
<span class="sourceLineNo">1048</span>            throw new IllegalArgumentException("null listener");<a name="line.1048"></a>
<span class="sourceLineNo">1049</span>        this.createListeners.add(listener);<a name="line.1049"></a>
<span class="sourceLineNo">1050</span>    }<a name="line.1050"></a>
<span class="sourceLineNo">1051</span><a name="line.1051"></a>
<span class="sourceLineNo">1052</span>    /**<a name="line.1052"></a>
<span class="sourceLineNo">1053</span>     * Remove an {@link CreateListener} from this transaction.<a name="line.1053"></a>
<span class="sourceLineNo">1054</span>     *<a name="line.1054"></a>
<span class="sourceLineNo">1055</span>     * @param listener the listener to remove<a name="line.1055"></a>
<span class="sourceLineNo">1056</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1056"></a>
<span class="sourceLineNo">1057</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1057"></a>
<span class="sourceLineNo">1058</span>     */<a name="line.1058"></a>
<span class="sourceLineNo">1059</span>    public synchronized void removeCreateListener(CreateListener listener) {<a name="line.1059"></a>
<span class="sourceLineNo">1060</span>        if (this.stale)<a name="line.1060"></a>
<span class="sourceLineNo">1061</span>            throw new StaleTransactionException(this);<a name="line.1061"></a>
<span class="sourceLineNo">1062</span>        if (listener == null)<a name="line.1062"></a>
<span class="sourceLineNo">1063</span>            throw new IllegalArgumentException("null listener");<a name="line.1063"></a>
<span class="sourceLineNo">1064</span>        this.createListeners.remove(listener);<a name="line.1064"></a>
<span class="sourceLineNo">1065</span>    }<a name="line.1065"></a>
<span class="sourceLineNo">1066</span><a name="line.1066"></a>
<span class="sourceLineNo">1067</span>    /**<a name="line.1067"></a>
<span class="sourceLineNo">1068</span>     * Add a {@link DeleteListener} to this transaction.<a name="line.1068"></a>
<span class="sourceLineNo">1069</span>     *<a name="line.1069"></a>
<span class="sourceLineNo">1070</span>     * @param listener the listener to add<a name="line.1070"></a>
<span class="sourceLineNo">1071</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1071"></a>
<span class="sourceLineNo">1072</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1072"></a>
<span class="sourceLineNo">1073</span>     */<a name="line.1073"></a>
<span class="sourceLineNo">1074</span>    public synchronized void addDeleteListener(DeleteListener listener) {<a name="line.1074"></a>
<span class="sourceLineNo">1075</span>        if (this.stale)<a name="line.1075"></a>
<span class="sourceLineNo">1076</span>            throw new StaleTransactionException(this);<a name="line.1076"></a>
<span class="sourceLineNo">1077</span>        if (listener == null)<a name="line.1077"></a>
<span class="sourceLineNo">1078</span>            throw new IllegalArgumentException("null listener");<a name="line.1078"></a>
<span class="sourceLineNo">1079</span>        this.deleteListeners.add(listener);<a name="line.1079"></a>
<span class="sourceLineNo">1080</span>    }<a name="line.1080"></a>
<span class="sourceLineNo">1081</span><a name="line.1081"></a>
<span class="sourceLineNo">1082</span>    /**<a name="line.1082"></a>
<span class="sourceLineNo">1083</span>     * Remove an {@link DeleteListener} from this transaction.<a name="line.1083"></a>
<span class="sourceLineNo">1084</span>     *<a name="line.1084"></a>
<span class="sourceLineNo">1085</span>     * @param listener the listener to remove<a name="line.1085"></a>
<span class="sourceLineNo">1086</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1086"></a>
<span class="sourceLineNo">1087</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1087"></a>
<span class="sourceLineNo">1088</span>     */<a name="line.1088"></a>
<span class="sourceLineNo">1089</span>    public synchronized void removeDeleteListener(DeleteListener listener) {<a name="line.1089"></a>
<span class="sourceLineNo">1090</span>        if (this.stale)<a name="line.1090"></a>
<span class="sourceLineNo">1091</span>            throw new StaleTransactionException(this);<a name="line.1091"></a>
<span class="sourceLineNo">1092</span>        if (listener == null)<a name="line.1092"></a>
<span class="sourceLineNo">1093</span>            throw new IllegalArgumentException("null listener");<a name="line.1093"></a>
<span class="sourceLineNo">1094</span>        this.deleteListeners.remove(listener);<a name="line.1094"></a>
<span class="sourceLineNo">1095</span>    }<a name="line.1095"></a>
<span class="sourceLineNo">1096</span><a name="line.1096"></a>
<span class="sourceLineNo">1097</span>// Object Versioning<a name="line.1097"></a>
<span class="sourceLineNo">1098</span><a name="line.1098"></a>
<span class="sourceLineNo">1099</span>    /**<a name="line.1099"></a>
<span class="sourceLineNo">1100</span>     * Read the current schema version of the given object. Does not change the object's version.<a name="line.1100"></a>
<span class="sourceLineNo">1101</span>     *<a name="line.1101"></a>
<span class="sourceLineNo">1102</span>     * @param id object id<a name="line.1102"></a>
<span class="sourceLineNo">1103</span>     * @return object's current schema version<a name="line.1103"></a>
<span class="sourceLineNo">1104</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1104"></a>
<span class="sourceLineNo">1105</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1105"></a>
<span class="sourceLineNo">1106</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1106"></a>
<span class="sourceLineNo">1107</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1107"></a>
<span class="sourceLineNo">1108</span>     */<a name="line.1108"></a>
<span class="sourceLineNo">1109</span>    public synchronized int getSchemaVersion(ObjId id) {<a name="line.1109"></a>
<span class="sourceLineNo">1110</span><a name="line.1110"></a>
<span class="sourceLineNo">1111</span>        // Sanity check<a name="line.1111"></a>
<span class="sourceLineNo">1112</span>        if (this.stale)<a name="line.1112"></a>
<span class="sourceLineNo">1113</span>            throw new StaleTransactionException(this);<a name="line.1113"></a>
<span class="sourceLineNo">1114</span>        if (id == null)<a name="line.1114"></a>
<span class="sourceLineNo">1115</span>            throw new IllegalArgumentException("null id");<a name="line.1115"></a>
<span class="sourceLineNo">1116</span><a name="line.1116"></a>
<span class="sourceLineNo">1117</span>        // Get object version<a name="line.1117"></a>
<span class="sourceLineNo">1118</span>        return this.getObjectInfo(id, false).getVersion();<a name="line.1118"></a>
<span class="sourceLineNo">1119</span>    }<a name="line.1119"></a>
<span class="sourceLineNo">1120</span><a name="line.1120"></a>
<span class="sourceLineNo">1121</span>    /**<a name="line.1121"></a>
<span class="sourceLineNo">1122</span>     * Change the schema version of the specified object, if necessary, so that its version matches<a name="line.1122"></a>
<span class="sourceLineNo">1123</span>     * {@linkplain #getSchema() the schema version associated with this transaction}.<a name="line.1123"></a>
<span class="sourceLineNo">1124</span>     *<a name="line.1124"></a>
<span class="sourceLineNo">1125</span>     * &lt;p&gt;<a name="line.1125"></a>
<span class="sourceLineNo">1126</span>     * If a schema change occurs, any registered {@link VersionChangeListener}s will be notified prior<a name="line.1126"></a>
<span class="sourceLineNo">1127</span>     * to this method returning.<a name="line.1127"></a>
<span class="sourceLineNo">1128</span>     * &lt;/p&gt;<a name="line.1128"></a>
<span class="sourceLineNo">1129</span>     *<a name="line.1129"></a>
<span class="sourceLineNo">1130</span>     * @param id object ID of the object to delete<a name="line.1130"></a>
<span class="sourceLineNo">1131</span>     * @return true if the object schema version was changed, otherwise false<a name="line.1131"></a>
<span class="sourceLineNo">1132</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1132"></a>
<span class="sourceLineNo">1133</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.1133"></a>
<span class="sourceLineNo">1134</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1134"></a>
<span class="sourceLineNo">1135</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1135"></a>
<span class="sourceLineNo">1136</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1136"></a>
<span class="sourceLineNo">1137</span>     * @throws TypeNotInSchemaVersionException if the object version could not be updated because the object's type<a name="line.1137"></a>
<span class="sourceLineNo">1138</span>     *   does not exist in the schema version associated with this transaction<a name="line.1138"></a>
<span class="sourceLineNo">1139</span>     * @see #getSchemaVersion(ObjId)<a name="line.1139"></a>
<span class="sourceLineNo">1140</span>     */<a name="line.1140"></a>
<span class="sourceLineNo">1141</span>    public synchronized boolean updateSchemaVersion(ObjId id) {<a name="line.1141"></a>
<span class="sourceLineNo">1142</span><a name="line.1142"></a>
<span class="sourceLineNo">1143</span>        // Sanity check<a name="line.1143"></a>
<span class="sourceLineNo">1144</span>        if (id == null)<a name="line.1144"></a>
<span class="sourceLineNo">1145</span>            throw new IllegalArgumentException("null id");<a name="line.1145"></a>
<span class="sourceLineNo">1146</span>        if (this.stale)<a name="line.1146"></a>
<span class="sourceLineNo">1147</span>            throw new StaleTransactionException(this);<a name="line.1147"></a>
<span class="sourceLineNo">1148</span>        if (this.readOnly)<a name="line.1148"></a>
<span class="sourceLineNo">1149</span>            throw new ReadOnlyTransactionException(this);<a name="line.1149"></a>
<span class="sourceLineNo">1150</span><a name="line.1150"></a>
<span class="sourceLineNo">1151</span>        // Get object info<a name="line.1151"></a>
<span class="sourceLineNo">1152</span>        final ObjInfo info = this.getObjectInfo(id, false);<a name="line.1152"></a>
<span class="sourceLineNo">1153</span>        if (info.getVersion() == this.schema.versionNumber)<a name="line.1153"></a>
<span class="sourceLineNo">1154</span>            return false;<a name="line.1154"></a>
<span class="sourceLineNo">1155</span><a name="line.1155"></a>
<span class="sourceLineNo">1156</span>        // Update schema version<a name="line.1156"></a>
<span class="sourceLineNo">1157</span>        this.mutateAndNotify(new Mutation&lt;Void&gt;() {<a name="line.1157"></a>
<span class="sourceLineNo">1158</span>            @Override<a name="line.1158"></a>
<span class="sourceLineNo">1159</span>            public Void mutate() {<a name="line.1159"></a>
<span class="sourceLineNo">1160</span>                Transaction.this.updateVersion(info, Transaction.this.schema);<a name="line.1160"></a>
<span class="sourceLineNo">1161</span>                return null;<a name="line.1161"></a>
<span class="sourceLineNo">1162</span>            }<a name="line.1162"></a>
<span class="sourceLineNo">1163</span>        });<a name="line.1163"></a>
<span class="sourceLineNo">1164</span>        return true;<a name="line.1164"></a>
<span class="sourceLineNo">1165</span>    }<a name="line.1165"></a>
<span class="sourceLineNo">1166</span><a name="line.1166"></a>
<span class="sourceLineNo">1167</span>    /**<a name="line.1167"></a>
<span class="sourceLineNo">1168</span>     * Update object to the current schema version.<a name="line.1168"></a>
<span class="sourceLineNo">1169</span>     *<a name="line.1169"></a>
<span class="sourceLineNo">1170</span>     * @param info original object info<a name="line.1170"></a>
<span class="sourceLineNo">1171</span>     * @param targetVersion version to change to<a name="line.1171"></a>
<span class="sourceLineNo">1172</span>     */<a name="line.1172"></a>
<span class="sourceLineNo">1173</span>    private synchronized void updateVersion(final ObjInfo info, final Schema targetVersion) {<a name="line.1173"></a>
<span class="sourceLineNo">1174</span><a name="line.1174"></a>
<span class="sourceLineNo">1175</span>        // Get version numbers<a name="line.1175"></a>
<span class="sourceLineNo">1176</span>        final ObjId id = info.getId();<a name="line.1176"></a>
<span class="sourceLineNo">1177</span>        final int oldVersion = info.getVersion();<a name="line.1177"></a>
<span class="sourceLineNo">1178</span>        final int newVersion = targetVersion.versionNumber;<a name="line.1178"></a>
<span class="sourceLineNo">1179</span><a name="line.1179"></a>
<span class="sourceLineNo">1180</span>        // Sanity check<a name="line.1180"></a>
<span class="sourceLineNo">1181</span>        assert this.schemas.getVersion(targetVersion.versionNumber) == targetVersion;<a name="line.1181"></a>
<span class="sourceLineNo">1182</span>        if (newVersion == oldVersion)<a name="line.1182"></a>
<span class="sourceLineNo">1183</span>            throw new IllegalArgumentException("object already at version " + newVersion);<a name="line.1183"></a>
<span class="sourceLineNo">1184</span>        if (this.readOnly)<a name="line.1184"></a>
<span class="sourceLineNo">1185</span>            throw new ReadOnlyTransactionException(this);<a name="line.1185"></a>
<span class="sourceLineNo">1186</span><a name="line.1186"></a>
<span class="sourceLineNo">1187</span>        // Get old and new types<a name="line.1187"></a>
<span class="sourceLineNo">1188</span>        final ObjType oldType = info.getObjType();<a name="line.1188"></a>
<span class="sourceLineNo">1189</span>        final ObjType newType;<a name="line.1189"></a>
<span class="sourceLineNo">1190</span>        try {<a name="line.1190"></a>
<span class="sourceLineNo">1191</span>            newType = targetVersion.getObjType(id.getStorageId());<a name="line.1191"></a>
<span class="sourceLineNo">1192</span>        } catch (UnknownTypeException e) {<a name="line.1192"></a>
<span class="sourceLineNo">1193</span>            throw (TypeNotInSchemaVersionException)new TypeNotInSchemaVersionException(id, newVersion).initCause(e);<a name="line.1193"></a>
<span class="sourceLineNo">1194</span>        }<a name="line.1194"></a>
<span class="sourceLineNo">1195</span><a name="line.1195"></a>
<span class="sourceLineNo">1196</span>        // Gather removed fields' values here for user migration if any VersionChangeListeners are registered<a name="line.1196"></a>
<span class="sourceLineNo">1197</span>        final TreeMap&lt;Integer, Object&gt; oldValueMap = !this.versionChangeListeners.isEmpty() ?<a name="line.1197"></a>
<span class="sourceLineNo">1198</span>          new TreeMap&lt;Integer, Object&gt;() : null;<a name="line.1198"></a>
<span class="sourceLineNo">1199</span><a name="line.1199"></a>
<span class="sourceLineNo">1200</span>    //////// Remove the index entries corresponding to removed composite indexes<a name="line.1200"></a>
<span class="sourceLineNo">1201</span><a name="line.1201"></a>
<span class="sourceLineNo">1202</span>        // Get composite index storage IDs (old or new)<a name="line.1202"></a>
<span class="sourceLineNo">1203</span>        final TreeSet&lt;Integer&gt; compositeIndexStorageIds = new TreeSet&lt;&gt;();<a name="line.1203"></a>
<span class="sourceLineNo">1204</span>        compositeIndexStorageIds.addAll(oldType.compositeIndexes.keySet());<a name="line.1204"></a>
<span class="sourceLineNo">1205</span>        compositeIndexStorageIds.addAll(newType.compositeIndexes.keySet());<a name="line.1205"></a>
<span class="sourceLineNo">1206</span><a name="line.1206"></a>
<span class="sourceLineNo">1207</span>        // Remove index entries for composite indexes that are going away<a name="line.1207"></a>
<span class="sourceLineNo">1208</span>        for (CompositeIndex index : oldType.compositeIndexes.values()) {<a name="line.1208"></a>
<span class="sourceLineNo">1209</span>            if (!newType.compositeIndexes.containsKey(index.storageId))<a name="line.1209"></a>
<span class="sourceLineNo">1210</span>                this.kvt.remove(this.buildCompositeIndexEntry(id, index));<a name="line.1210"></a>
<span class="sourceLineNo">1211</span>        }<a name="line.1211"></a>
<span class="sourceLineNo">1212</span><a name="line.1212"></a>
<span class="sourceLineNo">1213</span>    //////// Update counter fields<a name="line.1213"></a>
<span class="sourceLineNo">1214</span><a name="line.1214"></a>
<span class="sourceLineNo">1215</span>        // Get counter field storage IDs (old or new)<a name="line.1215"></a>
<span class="sourceLineNo">1216</span>        final TreeSet&lt;Integer&gt; counterFieldStorageIds = new TreeSet&lt;&gt;();<a name="line.1216"></a>
<span class="sourceLineNo">1217</span>        counterFieldStorageIds.addAll(oldType.counterFields.keySet());<a name="line.1217"></a>
<span class="sourceLineNo">1218</span>        counterFieldStorageIds.addAll(newType.counterFields.keySet());<a name="line.1218"></a>
<span class="sourceLineNo">1219</span><a name="line.1219"></a>
<span class="sourceLineNo">1220</span>        // Update counter fields<a name="line.1220"></a>
<span class="sourceLineNo">1221</span>        for (int storageId : counterFieldStorageIds) {<a name="line.1221"></a>
<span class="sourceLineNo">1222</span><a name="line.1222"></a>
<span class="sourceLineNo">1223</span>            // Get old and new counter fields having this storage ID<a name="line.1223"></a>
<span class="sourceLineNo">1224</span>            final CounterField oldField = oldType.counterFields.get(storageId);<a name="line.1224"></a>
<span class="sourceLineNo">1225</span>            final CounterField newField = newType.counterFields.get(storageId);<a name="line.1225"></a>
<span class="sourceLineNo">1226</span><a name="line.1226"></a>
<span class="sourceLineNo">1227</span>            // Save old field values for version change notification<a name="line.1227"></a>
<span class="sourceLineNo">1228</span>            final byte[] key = Field.buildKey(id, storageId);<a name="line.1228"></a>
<span class="sourceLineNo">1229</span>            if (oldField != null &amp;&amp; oldValueMap != null) {<a name="line.1229"></a>
<span class="sourceLineNo">1230</span>                final byte[] oldValue = this.kvt.get(key);<a name="line.1230"></a>
<span class="sourceLineNo">1231</span>                final long value = oldValue != null ? this.kvt.decodeCounter(oldValue) : 0;<a name="line.1231"></a>
<span class="sourceLineNo">1232</span>                oldValueMap.put(storageId, value);<a name="line.1232"></a>
<span class="sourceLineNo">1233</span>            }<a name="line.1233"></a>
<span class="sourceLineNo">1234</span><a name="line.1234"></a>
<span class="sourceLineNo">1235</span>            // Remove old value if field has disappeared, otherwise leave alone<a name="line.1235"></a>
<span class="sourceLineNo">1236</span>            if (newField == null)<a name="line.1236"></a>
<span class="sourceLineNo">1237</span>                this.kvt.remove(key);<a name="line.1237"></a>
<span class="sourceLineNo">1238</span>        }<a name="line.1238"></a>
<span class="sourceLineNo">1239</span><a name="line.1239"></a>
<span class="sourceLineNo">1240</span>    //////// Update simple fields and corresponding index entries<a name="line.1240"></a>
<span class="sourceLineNo">1241</span><a name="line.1241"></a>
<span class="sourceLineNo">1242</span>        // Get simple field storage IDs (old or new)<a name="line.1242"></a>
<span class="sourceLineNo">1243</span>        final TreeSet&lt;Integer&gt; simpleFieldStorageIds = new TreeSet&lt;&gt;();<a name="line.1243"></a>
<span class="sourceLineNo">1244</span>        simpleFieldStorageIds.addAll(oldType.simpleFields.keySet());<a name="line.1244"></a>
<span class="sourceLineNo">1245</span>        simpleFieldStorageIds.addAll(newType.simpleFields.keySet());<a name="line.1245"></a>
<span class="sourceLineNo">1246</span><a name="line.1246"></a>
<span class="sourceLineNo">1247</span>        // Update simple fields<a name="line.1247"></a>
<span class="sourceLineNo">1248</span>        for (int storageId : simpleFieldStorageIds) {<a name="line.1248"></a>
<span class="sourceLineNo">1249</span><a name="line.1249"></a>
<span class="sourceLineNo">1250</span>            // Get old and new simple fields having this storage ID<a name="line.1250"></a>
<span class="sourceLineNo">1251</span>            final SimpleField&lt;?&gt; oldField = oldType.simpleFields.get(storageId);<a name="line.1251"></a>
<span class="sourceLineNo">1252</span>            final SimpleField&lt;?&gt; newField = newType.simpleFields.get(storageId);<a name="line.1252"></a>
<span class="sourceLineNo">1253</span><a name="line.1253"></a>
<span class="sourceLineNo">1254</span>            // Save old field values for version change notification<a name="line.1254"></a>
<span class="sourceLineNo">1255</span>            final byte[] key = Field.buildKey(id, storageId);<a name="line.1255"></a>
<span class="sourceLineNo">1256</span>            final byte[] oldValue = oldField != null ? this.kvt.get(key) : null;<a name="line.1256"></a>
<span class="sourceLineNo">1257</span>            if (oldField != null &amp;&amp; oldValueMap != null) {<a name="line.1257"></a>
<span class="sourceLineNo">1258</span>                final byte[] bytes = oldValue != null ? oldValue : oldField.fieldType.getDefaultValue();<a name="line.1258"></a>
<span class="sourceLineNo">1259</span>                final Object value = oldField.fieldType.read(new ByteReader(bytes));<a name="line.1259"></a>
<span class="sourceLineNo">1260</span>                oldValueMap.put(storageId, value);<a name="line.1260"></a>
<span class="sourceLineNo">1261</span>            }<a name="line.1261"></a>
<span class="sourceLineNo">1262</span><a name="line.1262"></a>
<span class="sourceLineNo">1263</span>            // If field is being removed, discard the old value<a name="line.1263"></a>
<span class="sourceLineNo">1264</span>            if (newField == null &amp;&amp; oldValue != null)<a name="line.1264"></a>
<span class="sourceLineNo">1265</span>                this.kvt.remove(key);<a name="line.1265"></a>
<span class="sourceLineNo">1266</span><a name="line.1266"></a>
<span class="sourceLineNo">1267</span>            // Remove old index entry if index removed in new version<a name="line.1267"></a>
<span class="sourceLineNo">1268</span>            if (oldField != null &amp;&amp; oldField.indexed &amp;&amp; (newField == null || !newField.indexed))<a name="line.1268"></a>
<span class="sourceLineNo">1269</span>                this.kvt.remove(Transaction.buildSimpleIndexEntry(oldField, id, oldValue));<a name="line.1269"></a>
<span class="sourceLineNo">1270</span><a name="line.1270"></a>
<span class="sourceLineNo">1271</span>            // Add new index entry if index added in new version<a name="line.1271"></a>
<span class="sourceLineNo">1272</span>            if (newField != null &amp;&amp; newField.indexed &amp;&amp; (oldField == null || !oldField.indexed))<a name="line.1272"></a>
<span class="sourceLineNo">1273</span>                this.kvt.put(Transaction.buildSimpleIndexEntry(newField, id, oldValue), ByteUtil.EMPTY);<a name="line.1273"></a>
<span class="sourceLineNo">1274</span>        }<a name="line.1274"></a>
<span class="sourceLineNo">1275</span><a name="line.1275"></a>
<span class="sourceLineNo">1276</span>    //////// Add composite index entries for newly added composite indexes<a name="line.1276"></a>
<span class="sourceLineNo">1277</span><a name="line.1277"></a>
<span class="sourceLineNo">1278</span>        // Add index entries for composite indexes that are newly added<a name="line.1278"></a>
<span class="sourceLineNo">1279</span>        for (CompositeIndex index : newType.compositeIndexes.values()) {<a name="line.1279"></a>
<span class="sourceLineNo">1280</span>            if (!oldType.compositeIndexes.containsKey(index.storageId))<a name="line.1280"></a>
<span class="sourceLineNo">1281</span>                this.kvt.put(this.buildCompositeIndexEntry(id, index), ByteUtil.EMPTY);<a name="line.1281"></a>
<span class="sourceLineNo">1282</span>        }<a name="line.1282"></a>
<span class="sourceLineNo">1283</span><a name="line.1283"></a>
<span class="sourceLineNo">1284</span>    //////// Update complex fields and corresponding index entries<a name="line.1284"></a>
<span class="sourceLineNo">1285</span><a name="line.1285"></a>
<span class="sourceLineNo">1286</span>        // Get complex field storage IDs (old or new)<a name="line.1286"></a>
<span class="sourceLineNo">1287</span>        final TreeSet&lt;Integer&gt; complexFieldStorageIds = new TreeSet&lt;&gt;();<a name="line.1287"></a>
<span class="sourceLineNo">1288</span>        complexFieldStorageIds.addAll(oldType.complexFields.keySet());<a name="line.1288"></a>
<span class="sourceLineNo">1289</span>        complexFieldStorageIds.addAll(newType.complexFields.keySet());<a name="line.1289"></a>
<span class="sourceLineNo">1290</span><a name="line.1290"></a>
<span class="sourceLineNo">1291</span>        // Notes:<a name="line.1291"></a>
<span class="sourceLineNo">1292</span>        //<a name="line.1292"></a>
<span class="sourceLineNo">1293</span>        // - The only changes we support are sub-field changes that don't affect the corresponding StorageInfo's<a name="line.1293"></a>
<span class="sourceLineNo">1294</span>        // - New complex fields do not need to be explicitly initialized because their initial state is to have zero KV pairs<a name="line.1294"></a>
<span class="sourceLineNo">1295</span>        //<a name="line.1295"></a>
<span class="sourceLineNo">1296</span>        for (int storageId : complexFieldStorageIds) {<a name="line.1296"></a>
<span class="sourceLineNo">1297</span><a name="line.1297"></a>
<span class="sourceLineNo">1298</span>            // Get old and new complex fields having this storage ID<a name="line.1298"></a>
<span class="sourceLineNo">1299</span>            final ComplexField&lt;?&gt; oldField = oldType.complexFields.get(storageId);<a name="line.1299"></a>
<span class="sourceLineNo">1300</span>            final ComplexField&lt;?&gt; newField = newType.complexFields.get(storageId);<a name="line.1300"></a>
<span class="sourceLineNo">1301</span><a name="line.1301"></a>
<span class="sourceLineNo">1302</span>            // If there is no old field, new field and any associated indexes are already initialized (i.e., they're empty)<a name="line.1302"></a>
<span class="sourceLineNo">1303</span>            if (oldField == null)<a name="line.1303"></a>
<span class="sourceLineNo">1304</span>                continue;<a name="line.1304"></a>
<span class="sourceLineNo">1305</span><a name="line.1305"></a>
<span class="sourceLineNo">1306</span>            // Save old field's value<a name="line.1306"></a>
<span class="sourceLineNo">1307</span>            if (oldField != null &amp;&amp; oldValueMap != null)<a name="line.1307"></a>
<span class="sourceLineNo">1308</span>                oldValueMap.put(storageId, oldField.getValueReadOnlyCopy(this, id));<a name="line.1308"></a>
<span class="sourceLineNo">1309</span><a name="line.1309"></a>
<span class="sourceLineNo">1310</span>            // If field is being removed, delete old field content, otherwise check if index entries should be added/removed<a name="line.1310"></a>
<span class="sourceLineNo">1311</span>            if (newField == null) {<a name="line.1311"></a>
<span class="sourceLineNo">1312</span>                oldField.removeIndexEntries(this, id);<a name="line.1312"></a>
<span class="sourceLineNo">1313</span>                oldField.deleteContent(this, id);<a name="line.1313"></a>
<span class="sourceLineNo">1314</span>            } else<a name="line.1314"></a>
<span class="sourceLineNo">1315</span>                newField.updateSubFieldIndexes(this, oldField, id);<a name="line.1315"></a>
<span class="sourceLineNo">1316</span>        }<a name="line.1316"></a>
<span class="sourceLineNo">1317</span><a name="line.1317"></a>
<span class="sourceLineNo">1318</span>    //////// Remove references that are no longer valid<a name="line.1318"></a>
<span class="sourceLineNo">1319</span><a name="line.1319"></a>
<span class="sourceLineNo">1320</span>        // Unreference any reference fields that refer to no-longer-valid object types<a name="line.1320"></a>
<span class="sourceLineNo">1321</span>        for (int storageId : simpleFieldStorageIds) {<a name="line.1321"></a>
<span class="sourceLineNo">1322</span><a name="line.1322"></a>
<span class="sourceLineNo">1323</span>            // Get storage IDs that are no longer allowed, if any<a name="line.1323"></a>
<span class="sourceLineNo">1324</span>            final TreeSet&lt;Integer&gt; removedObjectTypes = this.findRemovedObjectTypes(info, targetVersion, storageId);<a name="line.1324"></a>
<span class="sourceLineNo">1325</span>            if (removedObjectTypes == null)<a name="line.1325"></a>
<span class="sourceLineNo">1326</span>                continue;<a name="line.1326"></a>
<span class="sourceLineNo">1327</span><a name="line.1327"></a>
<span class="sourceLineNo">1328</span>            // Unreference field if needed<a name="line.1328"></a>
<span class="sourceLineNo">1329</span>            final ReferenceField oldField = (ReferenceField)oldType.simpleFields.get(storageId);<a name="line.1329"></a>
<span class="sourceLineNo">1330</span>            final ObjId ref = oldField.getValue(this, id);<a name="line.1330"></a>
<span class="sourceLineNo">1331</span>            if (ref != null &amp;&amp; removedObjectTypes.contains(ref.getStorageId()))<a name="line.1331"></a>
<span class="sourceLineNo">1332</span>                this.writeSimpleField(id, oldField.storageId, null, false);<a name="line.1332"></a>
<span class="sourceLineNo">1333</span>        }<a name="line.1333"></a>
<span class="sourceLineNo">1334</span><a name="line.1334"></a>
<span class="sourceLineNo">1335</span>        // Unreference any reference sub-fields of complex fields that refer to no-longer-valid object types<a name="line.1335"></a>
<span class="sourceLineNo">1336</span>        for (int storageId : complexFieldStorageIds) {<a name="line.1336"></a>
<span class="sourceLineNo">1337</span><a name="line.1337"></a>
<span class="sourceLineNo">1338</span>            // Get old and new complex fields having this storage ID<a name="line.1338"></a>
<span class="sourceLineNo">1339</span>            final ComplexField&lt;?&gt; oldField = oldType.complexFields.get(storageId);<a name="line.1339"></a>
<span class="sourceLineNo">1340</span>            final ComplexField&lt;?&gt; newField = newType.complexFields.get(storageId);<a name="line.1340"></a>
<span class="sourceLineNo">1341</span>            if (oldField == null || newField == null)<a name="line.1341"></a>
<span class="sourceLineNo">1342</span>                continue;<a name="line.1342"></a>
<span class="sourceLineNo">1343</span>            final List&lt;? extends SimpleField&lt;?&gt;&gt; oldSubFields = oldField.getSubFields();<a name="line.1343"></a>
<span class="sourceLineNo">1344</span>            final List&lt;? extends SimpleField&lt;?&gt;&gt; newSubFields = newField.getSubFields();<a name="line.1344"></a>
<span class="sourceLineNo">1345</span>            final int numSubFields = oldSubFields.size();<a name="line.1345"></a>
<span class="sourceLineNo">1346</span>            assert numSubFields == newSubFields.size();<a name="line.1346"></a>
<span class="sourceLineNo">1347</span><a name="line.1347"></a>
<span class="sourceLineNo">1348</span>            // Iterate over subfields<a name="line.1348"></a>
<span class="sourceLineNo">1349</span>            for (int i = 0; i &lt; numSubFields; i++) {<a name="line.1349"></a>
<span class="sourceLineNo">1350</span>                final SimpleField&lt;?&gt; oldSubField = oldSubFields.get(i);<a name="line.1350"></a>
<span class="sourceLineNo">1351</span>                final SimpleField&lt;?&gt; newSubField = newSubFields.get(i);<a name="line.1351"></a>
<span class="sourceLineNo">1352</span>                assert oldSubField.storageId == newSubField.storageId;<a name="line.1352"></a>
<span class="sourceLineNo">1353</span>                final int subStorageId = oldSubField.storageId;<a name="line.1353"></a>
<span class="sourceLineNo">1354</span><a name="line.1354"></a>
<span class="sourceLineNo">1355</span>                // Get storage IDs that are no longer allowed, if any<a name="line.1355"></a>
<span class="sourceLineNo">1356</span>                final TreeSet&lt;Integer&gt; removedObjectTypes = this.findRemovedObjectTypes(info, targetVersion, subStorageId);<a name="line.1356"></a>
<span class="sourceLineNo">1357</span>                if (removedObjectTypes == null)<a name="line.1357"></a>
<span class="sourceLineNo">1358</span>                    continue;<a name="line.1358"></a>
<span class="sourceLineNo">1359</span><a name="line.1359"></a>
<span class="sourceLineNo">1360</span>                // Unreference sub-fields as needed<a name="line.1360"></a>
<span class="sourceLineNo">1361</span>                oldField.unreferenceRemovedObjectTypes(this, id, (ReferenceField)oldSubField, removedObjectTypes);<a name="line.1361"></a>
<span class="sourceLineNo">1362</span>            }<a name="line.1362"></a>
<span class="sourceLineNo">1363</span>        }<a name="line.1363"></a>
<span class="sourceLineNo">1364</span><a name="line.1364"></a>
<span class="sourceLineNo">1365</span>    //////// Update object version and corresponding index entry<a name="line.1365"></a>
<span class="sourceLineNo">1366</span><a name="line.1366"></a>
<span class="sourceLineNo">1367</span>        // Update object version<a name="line.1367"></a>
<span class="sourceLineNo">1368</span>        ObjInfo.write(this, id, newVersion, info.isDeleteNotified());<a name="line.1368"></a>
<span class="sourceLineNo">1369</span><a name="line.1369"></a>
<span class="sourceLineNo">1370</span>        // Update object version index entry<a name="line.1370"></a>
<span class="sourceLineNo">1371</span>        this.kvt.remove(Database.buildVersionIndexKey(id, oldVersion));<a name="line.1371"></a>
<span class="sourceLineNo">1372</span>        this.kvt.put(Database.buildVersionIndexKey(id, newVersion), ByteUtil.EMPTY);<a name="line.1372"></a>
<span class="sourceLineNo">1373</span><a name="line.1373"></a>
<span class="sourceLineNo">1374</span>    //////// Notify listeners<a name="line.1374"></a>
<span class="sourceLineNo">1375</span><a name="line.1375"></a>
<span class="sourceLineNo">1376</span>        // Lock down old field values map<a name="line.1376"></a>
<span class="sourceLineNo">1377</span>        final NavigableMap&lt;Integer, Object&gt; readOnlyOldValuesMap = oldValueMap != null ?<a name="line.1377"></a>
<span class="sourceLineNo">1378</span>          Maps.unmodifiableNavigableMap(oldValueMap) : null;<a name="line.1378"></a>
<span class="sourceLineNo">1379</span><a name="line.1379"></a>
<span class="sourceLineNo">1380</span>        // Notify about version update<a name="line.1380"></a>
<span class="sourceLineNo">1381</span>        for (VersionChangeListener listener : this.versionChangeListeners)<a name="line.1381"></a>
<span class="sourceLineNo">1382</span>            listener.onVersionChange(this, id, oldVersion, newVersion, readOnlyOldValuesMap);<a name="line.1382"></a>
<span class="sourceLineNo">1383</span>    }<a name="line.1383"></a>
<span class="sourceLineNo">1384</span><a name="line.1384"></a>
<span class="sourceLineNo">1385</span>    /**<a name="line.1385"></a>
<span class="sourceLineNo">1386</span>     * Find storage ID's which are no longer allowed by a reference field when upgrading to the specified<a name="line.1386"></a>
<span class="sourceLineNo">1387</span>     * schema version and therefore need to be scrubbed during the upgrade.<a name="line.1387"></a>
<span class="sourceLineNo">1388</span>     */<a name="line.1388"></a>
<span class="sourceLineNo">1389</span>    private TreeSet&lt;Integer&gt; findRemovedObjectTypes(ObjInfo info, Schema newVersion, int storageId) {<a name="line.1389"></a>
<span class="sourceLineNo">1390</span><a name="line.1390"></a>
<span class="sourceLineNo">1391</span>        // Get old schema version<a name="line.1391"></a>
<span class="sourceLineNo">1392</span>        final Schema oldVersion = info.getSchema();<a name="line.1392"></a>
<span class="sourceLineNo">1393</span><a name="line.1393"></a>
<span class="sourceLineNo">1394</span>        // Get old and new object types<a name="line.1394"></a>
<span class="sourceLineNo">1395</span>        final ObjType oldType = info.getObjType();<a name="line.1395"></a>
<span class="sourceLineNo">1396</span>        final ObjType newType = newVersion.getObjType(info.getId().getStorageId());<a name="line.1396"></a>
<span class="sourceLineNo">1397</span><a name="line.1397"></a>
<span class="sourceLineNo">1398</span>        // Get old and new reference fields having specified storage ID<a name="line.1398"></a>
<span class="sourceLineNo">1399</span>        final ReferenceField oldField = oldType.referenceFields.get(storageId);<a name="line.1399"></a>
<span class="sourceLineNo">1400</span>        final ReferenceField newField = newType.referenceFields.get(storageId);<a name="line.1400"></a>
<span class="sourceLineNo">1401</span>        if (oldField == null || newField == null)<a name="line.1401"></a>
<span class="sourceLineNo">1402</span>            return null;<a name="line.1402"></a>
<span class="sourceLineNo">1403</span><a name="line.1403"></a>
<span class="sourceLineNo">1404</span>        // Check allowed storage IDs<a name="line.1404"></a>
<span class="sourceLineNo">1405</span>        final SortedSet&lt;Integer&gt; newObjectTypes = newField.getObjectTypes();<a name="line.1405"></a>
<span class="sourceLineNo">1406</span>        if (newObjectTypes == null)<a name="line.1406"></a>
<span class="sourceLineNo">1407</span>            return null;                                                // new field can refer to any type in any schema version<a name="line.1407"></a>
<span class="sourceLineNo">1408</span>        SortedSet&lt;Integer&gt; oldObjectTypes = oldField.getObjectTypes();<a name="line.1408"></a>
<span class="sourceLineNo">1409</span>        if (oldObjectTypes == null)<a name="line.1409"></a>
<span class="sourceLineNo">1410</span>            oldObjectTypes = this.schemas.objTypeStorageIds;            // old field can refer to any type in any schema version<a name="line.1410"></a>
<span class="sourceLineNo">1411</span><a name="line.1411"></a>
<span class="sourceLineNo">1412</span>        // Identify storage IDs which are were allowed by old field but are no longer allowed by new field<a name="line.1412"></a>
<span class="sourceLineNo">1413</span>        final TreeSet&lt;Integer&gt; removedObjectTypes = new TreeSet&lt;&gt;(oldObjectTypes);<a name="line.1413"></a>
<span class="sourceLineNo">1414</span>        removedObjectTypes.removeAll(newObjectTypes);<a name="line.1414"></a>
<span class="sourceLineNo">1415</span>        return !removedObjectTypes.isEmpty() ? removedObjectTypes : null;<a name="line.1415"></a>
<span class="sourceLineNo">1416</span>    }<a name="line.1416"></a>
<span class="sourceLineNo">1417</span><a name="line.1417"></a>
<span class="sourceLineNo">1418</span>    /**<a name="line.1418"></a>
<span class="sourceLineNo">1419</span>     * Query objects by schema version.<a name="line.1419"></a>
<span class="sourceLineNo">1420</span>     *<a name="line.1420"></a>
<span class="sourceLineNo">1421</span>     * @return read-only, real-time view of all database objects indexed by schema version<a name="line.1421"></a>
<span class="sourceLineNo">1422</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1422"></a>
<span class="sourceLineNo">1423</span>     */<a name="line.1423"></a>
<span class="sourceLineNo">1424</span>    public synchronized CoreIndex&lt;Integer, ObjId&gt; queryVersion() {<a name="line.1424"></a>
<span class="sourceLineNo">1425</span>        if (this.stale)<a name="line.1425"></a>
<span class="sourceLineNo">1426</span>            throw new StaleTransactionException(this);<a name="line.1426"></a>
<span class="sourceLineNo">1427</span>        return this.db.getVersionIndex(this);<a name="line.1427"></a>
<span class="sourceLineNo">1428</span>    }<a name="line.1428"></a>
<span class="sourceLineNo">1429</span><a name="line.1429"></a>
<span class="sourceLineNo">1430</span>    /**<a name="line.1430"></a>
<span class="sourceLineNo">1431</span>     * Add an {@link VersionChangeListener} to this transaction.<a name="line.1431"></a>
<span class="sourceLineNo">1432</span>     *<a name="line.1432"></a>
<span class="sourceLineNo">1433</span>     * @param listener the listener to add<a name="line.1433"></a>
<span class="sourceLineNo">1434</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1434"></a>
<span class="sourceLineNo">1435</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1435"></a>
<span class="sourceLineNo">1436</span>     */<a name="line.1436"></a>
<span class="sourceLineNo">1437</span>    public synchronized void addVersionChangeListener(VersionChangeListener listener) {<a name="line.1437"></a>
<span class="sourceLineNo">1438</span>        if (this.stale)<a name="line.1438"></a>
<span class="sourceLineNo">1439</span>            throw new StaleTransactionException(this);<a name="line.1439"></a>
<span class="sourceLineNo">1440</span>        if (listener == null)<a name="line.1440"></a>
<span class="sourceLineNo">1441</span>            throw new IllegalArgumentException("null listener");<a name="line.1441"></a>
<span class="sourceLineNo">1442</span>        this.versionChangeListeners.add(listener);<a name="line.1442"></a>
<span class="sourceLineNo">1443</span>    }<a name="line.1443"></a>
<span class="sourceLineNo">1444</span><a name="line.1444"></a>
<span class="sourceLineNo">1445</span>    /**<a name="line.1445"></a>
<span class="sourceLineNo">1446</span>     * Remove an {@link VersionChangeListener} from this transaction.<a name="line.1446"></a>
<span class="sourceLineNo">1447</span>     *<a name="line.1447"></a>
<span class="sourceLineNo">1448</span>     * @param listener the listener to remove<a name="line.1448"></a>
<span class="sourceLineNo">1449</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1449"></a>
<span class="sourceLineNo">1450</span>     * @throws IllegalArgumentException if {@code listener} is null<a name="line.1450"></a>
<span class="sourceLineNo">1451</span>     */<a name="line.1451"></a>
<span class="sourceLineNo">1452</span>    public synchronized void removeVersionChangeListener(VersionChangeListener listener) {<a name="line.1452"></a>
<span class="sourceLineNo">1453</span>        if (this.stale)<a name="line.1453"></a>
<span class="sourceLineNo">1454</span>            throw new StaleTransactionException(this);<a name="line.1454"></a>
<span class="sourceLineNo">1455</span>        if (listener == null)<a name="line.1455"></a>
<span class="sourceLineNo">1456</span>            throw new IllegalArgumentException("null listener");<a name="line.1456"></a>
<span class="sourceLineNo">1457</span>        this.versionChangeListeners.remove(listener);<a name="line.1457"></a>
<span class="sourceLineNo">1458</span>    }<a name="line.1458"></a>
<span class="sourceLineNo">1459</span><a name="line.1459"></a>
<span class="sourceLineNo">1460</span>// Object and Field Access<a name="line.1460"></a>
<span class="sourceLineNo">1461</span><a name="line.1461"></a>
<span class="sourceLineNo">1462</span>    /**<a name="line.1462"></a>
<span class="sourceLineNo">1463</span>     * Get all objects in the database.<a name="line.1463"></a>
<span class="sourceLineNo">1464</span>     *<a name="line.1464"></a>
<span class="sourceLineNo">1465</span>     * &lt;p&gt;<a name="line.1465"></a>
<span class="sourceLineNo">1466</span>     * The returned set includes objects from all schema versions. Use {@link #queryVersion} to<a name="line.1466"></a>
<span class="sourceLineNo">1467</span>     * find objects with a specific schema version.<a name="line.1467"></a>
<span class="sourceLineNo">1468</span>     * &lt;/p&gt;<a name="line.1468"></a>
<span class="sourceLineNo">1469</span>     *<a name="line.1469"></a>
<span class="sourceLineNo">1470</span>     * &lt;p&gt;<a name="line.1470"></a>
<span class="sourceLineNo">1471</span>     * The returned set is mutable, with the exception that {@link NavigableSet#add add()} is not supported.<a name="line.1471"></a>
<span class="sourceLineNo">1472</span>     * Deleting an element results in {@linkplain #delete deleting} the corresponding object.<a name="line.1472"></a>
<span class="sourceLineNo">1473</span>     * &lt;/p&gt;<a name="line.1473"></a>
<span class="sourceLineNo">1474</span>     *<a name="line.1474"></a>
<span class="sourceLineNo">1475</span>     * @return set containing all objects<a name="line.1475"></a>
<span class="sourceLineNo">1476</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1476"></a>
<span class="sourceLineNo">1477</span>     * @see #getAll(int)<a name="line.1477"></a>
<span class="sourceLineNo">1478</span>     */<a name="line.1478"></a>
<span class="sourceLineNo">1479</span>    public NavigableSet&lt;ObjId&gt; getAll() {<a name="line.1479"></a>
<span class="sourceLineNo">1480</span><a name="line.1480"></a>
<span class="sourceLineNo">1481</span>        // Sanity check<a name="line.1481"></a>
<span class="sourceLineNo">1482</span>        if (this.stale)<a name="line.1482"></a>
<span class="sourceLineNo">1483</span>            throw new StaleTransactionException(this);<a name="line.1483"></a>
<span class="sourceLineNo">1484</span><a name="line.1484"></a>
<span class="sourceLineNo">1485</span>        // Return objects<a name="line.1485"></a>
<span class="sourceLineNo">1486</span>        return new ObjTypeSet(this);<a name="line.1486"></a>
<span class="sourceLineNo">1487</span>    }<a name="line.1487"></a>
<span class="sourceLineNo">1488</span><a name="line.1488"></a>
<span class="sourceLineNo">1489</span>    /**<a name="line.1489"></a>
<span class="sourceLineNo">1490</span>     * Get all objects whose object type has the specified storage ID.<a name="line.1490"></a>
<span class="sourceLineNo">1491</span>     *<a name="line.1491"></a>
<span class="sourceLineNo">1492</span>     * &lt;p&gt;<a name="line.1492"></a>
<span class="sourceLineNo">1493</span>     * The returned set is mutable, with the exception that {@link NavigableSet#add add()} is not supported.<a name="line.1493"></a>
<span class="sourceLineNo">1494</span>     * Deleting an element results in {@linkplain #delete deleting} the corresponding object.<a name="line.1494"></a>
<span class="sourceLineNo">1495</span>     * &lt;/p&gt;<a name="line.1495"></a>
<span class="sourceLineNo">1496</span>     *<a name="line.1496"></a>
<span class="sourceLineNo">1497</span>     * @param storageId object type storage ID<a name="line.1497"></a>
<span class="sourceLineNo">1498</span>     * @return set containing all objects having the specified storage ID<a name="line.1498"></a>
<span class="sourceLineNo">1499</span>     * @throws UnknownTypeException if {@code storageId} does not correspond to any known object type<a name="line.1499"></a>
<span class="sourceLineNo">1500</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1500"></a>
<span class="sourceLineNo">1501</span>     * @see #getAll()<a name="line.1501"></a>
<span class="sourceLineNo">1502</span>     */<a name="line.1502"></a>
<span class="sourceLineNo">1503</span>    public NavigableSet&lt;ObjId&gt; getAll(int storageId) {<a name="line.1503"></a>
<span class="sourceLineNo">1504</span><a name="line.1504"></a>
<span class="sourceLineNo">1505</span>        // Sanity check<a name="line.1505"></a>
<span class="sourceLineNo">1506</span>        if (this.stale)<a name="line.1506"></a>
<span class="sourceLineNo">1507</span>            throw new StaleTransactionException(this);<a name="line.1507"></a>
<span class="sourceLineNo">1508</span>        this.schemas.verifyStorageInfo(storageId, ObjTypeStorageInfo.class);<a name="line.1508"></a>
<span class="sourceLineNo">1509</span><a name="line.1509"></a>
<span class="sourceLineNo">1510</span>        // Return objects<a name="line.1510"></a>
<span class="sourceLineNo">1511</span>        return new ObjTypeSet(this, storageId);<a name="line.1511"></a>
<span class="sourceLineNo">1512</span>    }<a name="line.1512"></a>
<span class="sourceLineNo">1513</span><a name="line.1513"></a>
<span class="sourceLineNo">1514</span>    /**<a name="line.1514"></a>
<span class="sourceLineNo">1515</span>     * Read the value of a {@link SimpleField} from an object, optionally updating the object's schema version.<a name="line.1515"></a>
<span class="sourceLineNo">1516</span>     *<a name="line.1516"></a>
<span class="sourceLineNo">1517</span>     * &lt;p&gt;<a name="line.1517"></a>
<span class="sourceLineNo">1518</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1518"></a>
<span class="sourceLineNo">1519</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary, prior to<a name="line.1519"></a>
<span class="sourceLineNo">1520</span>     * reading the field.<a name="line.1520"></a>
<span class="sourceLineNo">1521</span>     * &lt;/p&gt;<a name="line.1521"></a>
<span class="sourceLineNo">1522</span>     *<a name="line.1522"></a>
<span class="sourceLineNo">1523</span>     * @param id object ID of the object<a name="line.1523"></a>
<span class="sourceLineNo">1524</span>     * @param storageId storage ID of the {@link SimpleField}<a name="line.1524"></a>
<span class="sourceLineNo">1525</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1525"></a>
<span class="sourceLineNo">1526</span>     * @return value of the field in the object<a name="line.1526"></a>
<span class="sourceLineNo">1527</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1527"></a>
<span class="sourceLineNo">1528</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1528"></a>
<span class="sourceLineNo">1529</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1529"></a>
<span class="sourceLineNo">1530</span>     * @throws UnknownFieldException if no {@link SimpleField} corresponding to {@code storageId} exists in the object<a name="line.1530"></a>
<span class="sourceLineNo">1531</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1531"></a>
<span class="sourceLineNo">1532</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1532"></a>
<span class="sourceLineNo">1533</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1533"></a>
<span class="sourceLineNo">1534</span>     */<a name="line.1534"></a>
<span class="sourceLineNo">1535</span>    public synchronized Object readSimpleField(ObjId id, int storageId, boolean updateVersion) {<a name="line.1535"></a>
<span class="sourceLineNo">1536</span><a name="line.1536"></a>
<span class="sourceLineNo">1537</span>        // Sanity check<a name="line.1537"></a>
<span class="sourceLineNo">1538</span>        if (this.stale)<a name="line.1538"></a>
<span class="sourceLineNo">1539</span>            throw new StaleTransactionException(this);<a name="line.1539"></a>
<span class="sourceLineNo">1540</span>        if (id == null)<a name="line.1540"></a>
<span class="sourceLineNo">1541</span>            throw new IllegalArgumentException("null id");<a name="line.1541"></a>
<span class="sourceLineNo">1542</span><a name="line.1542"></a>
<span class="sourceLineNo">1543</span>        // Get object info<a name="line.1543"></a>
<span class="sourceLineNo">1544</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1544"></a>
<span class="sourceLineNo">1545</span><a name="line.1545"></a>
<span class="sourceLineNo">1546</span>        // Find field<a name="line.1546"></a>
<span class="sourceLineNo">1547</span>        final SimpleField&lt;?&gt; field = info.getObjType().simpleFields.get(storageId);<a name="line.1547"></a>
<span class="sourceLineNo">1548</span>        if (field == null)<a name="line.1548"></a>
<span class="sourceLineNo">1549</span>            throw new UnknownFieldException(info.getObjType(), storageId, "simple field");<a name="line.1549"></a>
<span class="sourceLineNo">1550</span><a name="line.1550"></a>
<span class="sourceLineNo">1551</span>        // Read field<a name="line.1551"></a>
<span class="sourceLineNo">1552</span>        final byte[] key = field.buildKey(id);<a name="line.1552"></a>
<span class="sourceLineNo">1553</span>        final byte[] value = this.kvt.get(key);<a name="line.1553"></a>
<span class="sourceLineNo">1554</span><a name="line.1554"></a>
<span class="sourceLineNo">1555</span>        // Decode value<a name="line.1555"></a>
<span class="sourceLineNo">1556</span>        return field.fieldType.read(new ByteReader(value != null ? value : field.fieldType.getDefaultValue()));<a name="line.1556"></a>
<span class="sourceLineNo">1557</span>    }<a name="line.1557"></a>
<span class="sourceLineNo">1558</span><a name="line.1558"></a>
<span class="sourceLineNo">1559</span>    /**<a name="line.1559"></a>
<span class="sourceLineNo">1560</span>     * Change the value of a {@link SimpleField} in an object, optionally updating the object's schema version.<a name="line.1560"></a>
<span class="sourceLineNo">1561</span>     *<a name="line.1561"></a>
<span class="sourceLineNo">1562</span>     * &lt;p&gt;<a name="line.1562"></a>
<span class="sourceLineNo">1563</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1563"></a>
<span class="sourceLineNo">1564</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary, prior to<a name="line.1564"></a>
<span class="sourceLineNo">1565</span>     * writing the field.<a name="line.1565"></a>
<span class="sourceLineNo">1566</span>     * &lt;/p&gt;<a name="line.1566"></a>
<span class="sourceLineNo">1567</span>     *<a name="line.1567"></a>
<span class="sourceLineNo">1568</span>     * @param id object ID of the object<a name="line.1568"></a>
<span class="sourceLineNo">1569</span>     * @param storageId storage ID of the {@link SimpleField}<a name="line.1569"></a>
<span class="sourceLineNo">1570</span>     * @param value new value for the field<a name="line.1570"></a>
<span class="sourceLineNo">1571</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1571"></a>
<span class="sourceLineNo">1572</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1572"></a>
<span class="sourceLineNo">1573</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1573"></a>
<span class="sourceLineNo">1574</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1574"></a>
<span class="sourceLineNo">1575</span>     * @throws UnknownFieldException if no {@link SimpleField} corresponding to {@code storageId} exists in the object<a name="line.1575"></a>
<span class="sourceLineNo">1576</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1576"></a>
<span class="sourceLineNo">1577</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1577"></a>
<span class="sourceLineNo">1578</span>     * @throws IllegalArgumentException if {@code value} is not an appropriate value for the field<a name="line.1578"></a>
<span class="sourceLineNo">1579</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1579"></a>
<span class="sourceLineNo">1580</span>     */<a name="line.1580"></a>
<span class="sourceLineNo">1581</span>    public synchronized void writeSimpleField(final ObjId id,<a name="line.1581"></a>
<span class="sourceLineNo">1582</span>      final int storageId, final Object value, final boolean updateVersion) {<a name="line.1582"></a>
<span class="sourceLineNo">1583</span>        this.mutateAndNotify(id, new Mutation&lt;Void&gt;() {<a name="line.1583"></a>
<span class="sourceLineNo">1584</span>            @Override<a name="line.1584"></a>
<span class="sourceLineNo">1585</span>            public Void mutate() {<a name="line.1585"></a>
<span class="sourceLineNo">1586</span>                Transaction.this.doWriteSimpleField(id, storageId, value, updateVersion);<a name="line.1586"></a>
<span class="sourceLineNo">1587</span>                return null;<a name="line.1587"></a>
<span class="sourceLineNo">1588</span>            }<a name="line.1588"></a>
<span class="sourceLineNo">1589</span>        });<a name="line.1589"></a>
<span class="sourceLineNo">1590</span>    }<a name="line.1590"></a>
<span class="sourceLineNo">1591</span><a name="line.1591"></a>
<span class="sourceLineNo">1592</span>    private synchronized void doWriteSimpleField(ObjId id, int storageId, final Object newObj, boolean updateVersion) {<a name="line.1592"></a>
<span class="sourceLineNo">1593</span><a name="line.1593"></a>
<span class="sourceLineNo">1594</span>        // Get object info<a name="line.1594"></a>
<span class="sourceLineNo">1595</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1595"></a>
<span class="sourceLineNo">1596</span><a name="line.1596"></a>
<span class="sourceLineNo">1597</span>        // Find field<a name="line.1597"></a>
<span class="sourceLineNo">1598</span>        final SimpleField&lt;?&gt; field = info.getObjType().simpleFields.get(storageId);<a name="line.1598"></a>
<span class="sourceLineNo">1599</span>        if (field == null)<a name="line.1599"></a>
<span class="sourceLineNo">1600</span>            throw new UnknownFieldException(info.getObjType(), storageId, "simple field");<a name="line.1600"></a>
<span class="sourceLineNo">1601</span><a name="line.1601"></a>
<span class="sourceLineNo">1602</span>        // Get new value<a name="line.1602"></a>
<span class="sourceLineNo">1603</span>        final byte[] key = field.buildKey(id);<a name="line.1603"></a>
<span class="sourceLineNo">1604</span>        final byte[] newValue = field.encode(newObj);<a name="line.1604"></a>
<span class="sourceLineNo">1605</span><a name="line.1605"></a>
<span class="sourceLineNo">1606</span>        // Before setting the new value, read the old value if one of the following is true:<a name="line.1606"></a>
<span class="sourceLineNo">1607</span>        //  - The field is being monitored -&gt; we need to filter out "changes" that don't actuallly change anything<a name="line.1607"></a>
<span class="sourceLineNo">1608</span>        //  - The field is indexed -&gt; we need the old value so we can remove the old index entry<a name="line.1608"></a>
<span class="sourceLineNo">1609</span>        // If neither of the above is true, then there's no need to read the old value.<a name="line.1609"></a>
<span class="sourceLineNo">1610</span>        byte[] oldValue = null;<a name="line.1610"></a>
<span class="sourceLineNo">1611</span>        if (field.indexed || field.compositeIndexMap != null || this.hasFieldMonitor(id, field)) {<a name="line.1611"></a>
<span class="sourceLineNo">1612</span><a name="line.1612"></a>
<span class="sourceLineNo">1613</span>            // Get old value<a name="line.1613"></a>
<span class="sourceLineNo">1614</span>            oldValue = this.kvt.get(key);<a name="line.1614"></a>
<span class="sourceLineNo">1615</span><a name="line.1615"></a>
<span class="sourceLineNo">1616</span>            // Compare new to old value<a name="line.1616"></a>
<span class="sourceLineNo">1617</span>            if (oldValue != null ? newValue != null &amp;&amp; Arrays.equals(oldValue, newValue) : newValue == null)<a name="line.1617"></a>
<span class="sourceLineNo">1618</span>                return;<a name="line.1618"></a>
<span class="sourceLineNo">1619</span>        }<a name="line.1619"></a>
<span class="sourceLineNo">1620</span><a name="line.1620"></a>
<span class="sourceLineNo">1621</span>        // Update value<a name="line.1621"></a>
<span class="sourceLineNo">1622</span>        if (newValue != null)<a name="line.1622"></a>
<span class="sourceLineNo">1623</span>            this.kvt.put(key, newValue);<a name="line.1623"></a>
<span class="sourceLineNo">1624</span>        else<a name="line.1624"></a>
<span class="sourceLineNo">1625</span>            this.kvt.remove(key);<a name="line.1625"></a>
<span class="sourceLineNo">1626</span><a name="line.1626"></a>
<span class="sourceLineNo">1627</span>        // Update simple index, if any<a name="line.1627"></a>
<span class="sourceLineNo">1628</span>        if (field.indexed) {<a name="line.1628"></a>
<span class="sourceLineNo">1629</span>            this.kvt.remove(Transaction.buildSimpleIndexEntry(field, id, oldValue));<a name="line.1629"></a>
<span class="sourceLineNo">1630</span>            this.kvt.put(Transaction.buildSimpleIndexEntry(field, id, newValue), ByteUtil.EMPTY);<a name="line.1630"></a>
<span class="sourceLineNo">1631</span>        }<a name="line.1631"></a>
<span class="sourceLineNo">1632</span><a name="line.1632"></a>
<span class="sourceLineNo">1633</span>        // Update affected composite indexes, if any<a name="line.1633"></a>
<span class="sourceLineNo">1634</span>        if (field.compositeIndexMap != null) {<a name="line.1634"></a>
<span class="sourceLineNo">1635</span>            for (Map.Entry&lt;CompositeIndex, Integer&gt; entry : field.compositeIndexMap.entrySet()) {<a name="line.1635"></a>
<span class="sourceLineNo">1636</span>                final CompositeIndex index = entry.getKey();<a name="line.1636"></a>
<span class="sourceLineNo">1637</span>                final int fieldIndexOffset = entry.getValue();<a name="line.1637"></a>
<span class="sourceLineNo">1638</span><a name="line.1638"></a>
<span class="sourceLineNo">1639</span>                // Build old composite index entry<a name="line.1639"></a>
<span class="sourceLineNo">1640</span>                final ByteWriter oldWriter = new ByteWriter();<a name="line.1640"></a>
<span class="sourceLineNo">1641</span>                UnsignedIntEncoder.write(oldWriter, index.storageId);<a name="line.1641"></a>
<span class="sourceLineNo">1642</span>                int fieldStart = -1;<a name="line.1642"></a>
<span class="sourceLineNo">1643</span>                int fieldEnd = -1;<a name="line.1643"></a>
<span class="sourceLineNo">1644</span>                for (SimpleField&lt;?&gt; otherField : index.fields) {<a name="line.1644"></a>
<span class="sourceLineNo">1645</span>                    final byte[] otherValue;<a name="line.1645"></a>
<span class="sourceLineNo">1646</span>                    if (otherField == field) {<a name="line.1646"></a>
<span class="sourceLineNo">1647</span>                        fieldStart = oldWriter.getLength();<a name="line.1647"></a>
<span class="sourceLineNo">1648</span>                        otherValue = oldValue;<a name="line.1648"></a>
<span class="sourceLineNo">1649</span>                    } else<a name="line.1649"></a>
<span class="sourceLineNo">1650</span>                        otherValue = this.kvt.get(otherField.buildKey(id));         // can be null (if field has default value)<a name="line.1650"></a>
<span class="sourceLineNo">1651</span>                    oldWriter.write(otherValue != null ? otherValue : otherField.fieldType.getDefaultValue());<a name="line.1651"></a>
<span class="sourceLineNo">1652</span>                    if (otherField == field)<a name="line.1652"></a>
<span class="sourceLineNo">1653</span>                        fieldEnd = oldWriter.getLength();<a name="line.1653"></a>
<span class="sourceLineNo">1654</span>                }<a name="line.1654"></a>
<span class="sourceLineNo">1655</span>                assert fieldStart != -1;<a name="line.1655"></a>
<span class="sourceLineNo">1656</span>                assert fieldEnd != -1;<a name="line.1656"></a>
<span class="sourceLineNo">1657</span>                id.writeTo(oldWriter);<a name="line.1657"></a>
<span class="sourceLineNo">1658</span><a name="line.1658"></a>
<span class="sourceLineNo">1659</span>                // Remove old composite index entry<a name="line.1659"></a>
<span class="sourceLineNo">1660</span>                final byte[] oldIndexEntry = oldWriter.getBytes();<a name="line.1660"></a>
<span class="sourceLineNo">1661</span>                this.kvt.remove(oldIndexEntry);<a name="line.1661"></a>
<span class="sourceLineNo">1662</span><a name="line.1662"></a>
<span class="sourceLineNo">1663</span>                // Patch in new field value to create new composite index entry<a name="line.1663"></a>
<span class="sourceLineNo">1664</span>                final ByteWriter newWriter = new ByteWriter(oldIndexEntry.length);<a name="line.1664"></a>
<span class="sourceLineNo">1665</span>                newWriter.write(oldIndexEntry, 0, fieldStart);<a name="line.1665"></a>
<span class="sourceLineNo">1666</span>                newWriter.write(newValue != null ? newValue : field.fieldType.getDefaultValue());<a name="line.1666"></a>
<span class="sourceLineNo">1667</span>                newWriter.write(oldIndexEntry, fieldEnd, oldIndexEntry.length - fieldEnd);<a name="line.1667"></a>
<span class="sourceLineNo">1668</span><a name="line.1668"></a>
<span class="sourceLineNo">1669</span>                // Add new composite index entry<a name="line.1669"></a>
<span class="sourceLineNo">1670</span>                this.kvt.put(newWriter.getBytes(), ByteUtil.EMPTY);<a name="line.1670"></a>
<span class="sourceLineNo">1671</span>            }<a name="line.1671"></a>
<span class="sourceLineNo">1672</span>        }<a name="line.1672"></a>
<span class="sourceLineNo">1673</span><a name="line.1673"></a>
<span class="sourceLineNo">1674</span>        // Notify monitors<a name="line.1674"></a>
<span class="sourceLineNo">1675</span>        final Object oldObj = field.fieldType.read(new ByteReader(oldValue != null ? oldValue : field.fieldType.getDefaultValue()));<a name="line.1675"></a>
<span class="sourceLineNo">1676</span>        this.addFieldChangeNotification(new SimpleFieldChangeNotifier(field, id) {<a name="line.1676"></a>
<span class="sourceLineNo">1677</span>            @Override<a name="line.1677"></a>
<span class="sourceLineNo">1678</span>            @SuppressWarnings("unchecked")<a name="line.1678"></a>
<span class="sourceLineNo">1679</span>            public void notify(Transaction tx, SimpleFieldChangeListener listener, int[] path, NavigableSet&lt;ObjId&gt; referrers) {<a name="line.1679"></a>
<span class="sourceLineNo">1680</span>                listener.onSimpleFieldChange(tx, this.id, (SimpleField&lt;Object&gt;)field, path, referrers, oldObj, newObj);<a name="line.1680"></a>
<span class="sourceLineNo">1681</span>            }<a name="line.1681"></a>
<span class="sourceLineNo">1682</span>        });<a name="line.1682"></a>
<span class="sourceLineNo">1683</span>    }<a name="line.1683"></a>
<span class="sourceLineNo">1684</span><a name="line.1684"></a>
<span class="sourceLineNo">1685</span>    /**<a name="line.1685"></a>
<span class="sourceLineNo">1686</span>     * Bulid a simple index entry for the given field, object ID, and field value.<a name="line.1686"></a>
<span class="sourceLineNo">1687</span>     *<a name="line.1687"></a>
<span class="sourceLineNo">1688</span>     * @param field simple field<a name="line.1688"></a>
<span class="sourceLineNo">1689</span>     * @param id ID of object containing the field<a name="line.1689"></a>
<span class="sourceLineNo">1690</span>     * @param value encoded field value, or null for default value<a name="line.1690"></a>
<span class="sourceLineNo">1691</span>     * @return index key<a name="line.1691"></a>
<span class="sourceLineNo">1692</span>     */<a name="line.1692"></a>
<span class="sourceLineNo">1693</span>    private static byte[] buildSimpleIndexEntry(SimpleField&lt;?&gt; field, ObjId id, byte[] value) {<a name="line.1693"></a>
<span class="sourceLineNo">1694</span>        final ByteWriter writer = new ByteWriter();<a name="line.1694"></a>
<span class="sourceLineNo">1695</span>        UnsignedIntEncoder.write(writer, field.storageId);<a name="line.1695"></a>
<span class="sourceLineNo">1696</span>        writer.write(value != null ? value : field.fieldType.getDefaultValue());<a name="line.1696"></a>
<span class="sourceLineNo">1697</span>        id.writeTo(writer);<a name="line.1697"></a>
<span class="sourceLineNo">1698</span>        return writer.getBytes();<a name="line.1698"></a>
<span class="sourceLineNo">1699</span>    }<a name="line.1699"></a>
<span class="sourceLineNo">1700</span><a name="line.1700"></a>
<span class="sourceLineNo">1701</span>    /**<a name="line.1701"></a>
<span class="sourceLineNo">1702</span>     * Read the value of a {@link CounterField} from an object, optionally updating the object's schema version.<a name="line.1702"></a>
<span class="sourceLineNo">1703</span>     *<a name="line.1703"></a>
<span class="sourceLineNo">1704</span>     * &lt;p&gt;<a name="line.1704"></a>
<span class="sourceLineNo">1705</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1705"></a>
<span class="sourceLineNo">1706</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary, prior to<a name="line.1706"></a>
<span class="sourceLineNo">1707</span>     * reading the field.<a name="line.1707"></a>
<span class="sourceLineNo">1708</span>     * &lt;/p&gt;<a name="line.1708"></a>
<span class="sourceLineNo">1709</span>     *<a name="line.1709"></a>
<span class="sourceLineNo">1710</span>     * @param id object ID of the object<a name="line.1710"></a>
<span class="sourceLineNo">1711</span>     * @param storageId storage ID of the {@link CounterField}<a name="line.1711"></a>
<span class="sourceLineNo">1712</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1712"></a>
<span class="sourceLineNo">1713</span>     * @return value of the counter in the object<a name="line.1713"></a>
<span class="sourceLineNo">1714</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1714"></a>
<span class="sourceLineNo">1715</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1715"></a>
<span class="sourceLineNo">1716</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1716"></a>
<span class="sourceLineNo">1717</span>     * @throws UnknownFieldException if no {@link CounterField} corresponding to {@code storageId} exists in the object<a name="line.1717"></a>
<span class="sourceLineNo">1718</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1718"></a>
<span class="sourceLineNo">1719</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1719"></a>
<span class="sourceLineNo">1720</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1720"></a>
<span class="sourceLineNo">1721</span>     */<a name="line.1721"></a>
<span class="sourceLineNo">1722</span>    public synchronized long readCounterField(ObjId id, int storageId, boolean updateVersion) {<a name="line.1722"></a>
<span class="sourceLineNo">1723</span><a name="line.1723"></a>
<span class="sourceLineNo">1724</span>        // Sanity check<a name="line.1724"></a>
<span class="sourceLineNo">1725</span>        if (this.stale)<a name="line.1725"></a>
<span class="sourceLineNo">1726</span>            throw new StaleTransactionException(this);<a name="line.1726"></a>
<span class="sourceLineNo">1727</span>        if (id == null)<a name="line.1727"></a>
<span class="sourceLineNo">1728</span>            throw new IllegalArgumentException("null id");<a name="line.1728"></a>
<span class="sourceLineNo">1729</span><a name="line.1729"></a>
<span class="sourceLineNo">1730</span>        // Get object info<a name="line.1730"></a>
<span class="sourceLineNo">1731</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1731"></a>
<span class="sourceLineNo">1732</span><a name="line.1732"></a>
<span class="sourceLineNo">1733</span>        // Find field<a name="line.1733"></a>
<span class="sourceLineNo">1734</span>        final CounterField field = info.getObjType().counterFields.get(storageId);<a name="line.1734"></a>
<span class="sourceLineNo">1735</span>        if (field == null)<a name="line.1735"></a>
<span class="sourceLineNo">1736</span>            throw new UnknownFieldException(info.getObjType(), storageId, "counter field");<a name="line.1736"></a>
<span class="sourceLineNo">1737</span><a name="line.1737"></a>
<span class="sourceLineNo">1738</span>        // Read field<a name="line.1738"></a>
<span class="sourceLineNo">1739</span>        final byte[] key = field.buildKey(id);<a name="line.1739"></a>
<span class="sourceLineNo">1740</span>        final byte[] value = this.kvt.get(key);<a name="line.1740"></a>
<span class="sourceLineNo">1741</span><a name="line.1741"></a>
<span class="sourceLineNo">1742</span>        // Decode value<a name="line.1742"></a>
<span class="sourceLineNo">1743</span>        return value != null ? this.kvt.decodeCounter(value) : 0;<a name="line.1743"></a>
<span class="sourceLineNo">1744</span>    }<a name="line.1744"></a>
<span class="sourceLineNo">1745</span><a name="line.1745"></a>
<span class="sourceLineNo">1746</span>    /**<a name="line.1746"></a>
<span class="sourceLineNo">1747</span>     * Set the value of a {@link CounterField} in an object, optionally updating the object's schema version.<a name="line.1747"></a>
<span class="sourceLineNo">1748</span>     *<a name="line.1748"></a>
<span class="sourceLineNo">1749</span>     * &lt;p&gt;<a name="line.1749"></a>
<span class="sourceLineNo">1750</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1750"></a>
<span class="sourceLineNo">1751</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary, prior to<a name="line.1751"></a>
<span class="sourceLineNo">1752</span>     * writing the field.<a name="line.1752"></a>
<span class="sourceLineNo">1753</span>     * &lt;/p&gt;<a name="line.1753"></a>
<span class="sourceLineNo">1754</span>     *<a name="line.1754"></a>
<span class="sourceLineNo">1755</span>     * @param id object ID of the object<a name="line.1755"></a>
<span class="sourceLineNo">1756</span>     * @param storageId storage ID of the {@link CounterField}<a name="line.1756"></a>
<span class="sourceLineNo">1757</span>     * @param value new counter value<a name="line.1757"></a>
<span class="sourceLineNo">1758</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1758"></a>
<span class="sourceLineNo">1759</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1759"></a>
<span class="sourceLineNo">1760</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1760"></a>
<span class="sourceLineNo">1761</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1761"></a>
<span class="sourceLineNo">1762</span>     * @throws UnknownFieldException if no {@link CounterField} corresponding to {@code storageId} exists in the object<a name="line.1762"></a>
<span class="sourceLineNo">1763</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1763"></a>
<span class="sourceLineNo">1764</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1764"></a>
<span class="sourceLineNo">1765</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1765"></a>
<span class="sourceLineNo">1766</span>     */<a name="line.1766"></a>
<span class="sourceLineNo">1767</span>    public synchronized void writeCounterField(final ObjId id, final int storageId, final long value, final boolean updateVersion) {<a name="line.1767"></a>
<span class="sourceLineNo">1768</span><a name="line.1768"></a>
<span class="sourceLineNo">1769</span>        // Sanity check<a name="line.1769"></a>
<span class="sourceLineNo">1770</span>        if (this.stale)<a name="line.1770"></a>
<span class="sourceLineNo">1771</span>            throw new StaleTransactionException(this);<a name="line.1771"></a>
<span class="sourceLineNo">1772</span>        if (id == null)<a name="line.1772"></a>
<span class="sourceLineNo">1773</span>            throw new IllegalArgumentException("null id");<a name="line.1773"></a>
<span class="sourceLineNo">1774</span>        if (this.readOnly)<a name="line.1774"></a>
<span class="sourceLineNo">1775</span>            throw new ReadOnlyTransactionException(this);<a name="line.1775"></a>
<span class="sourceLineNo">1776</span><a name="line.1776"></a>
<span class="sourceLineNo">1777</span>        // Get object info<a name="line.1777"></a>
<span class="sourceLineNo">1778</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1778"></a>
<span class="sourceLineNo">1779</span><a name="line.1779"></a>
<span class="sourceLineNo">1780</span>        // Find field<a name="line.1780"></a>
<span class="sourceLineNo">1781</span>        final CounterField field = info.getObjType().counterFields.get(storageId);<a name="line.1781"></a>
<span class="sourceLineNo">1782</span>        if (field == null)<a name="line.1782"></a>
<span class="sourceLineNo">1783</span>            throw new UnknownFieldException(info.getObjType(), storageId, "counter field");<a name="line.1783"></a>
<span class="sourceLineNo">1784</span><a name="line.1784"></a>
<span class="sourceLineNo">1785</span>        // Set value<a name="line.1785"></a>
<span class="sourceLineNo">1786</span>        final byte[] key = field.buildKey(id);<a name="line.1786"></a>
<span class="sourceLineNo">1787</span>        this.kvt.put(key, this.kvt.encodeCounter(value));<a name="line.1787"></a>
<span class="sourceLineNo">1788</span>    }<a name="line.1788"></a>
<span class="sourceLineNo">1789</span><a name="line.1789"></a>
<span class="sourceLineNo">1790</span>    /**<a name="line.1790"></a>
<span class="sourceLineNo">1791</span>     * Adjust the value of a {@link CounterField} in an object by some amount, optionally updating the object's schema version.<a name="line.1791"></a>
<span class="sourceLineNo">1792</span>     *<a name="line.1792"></a>
<span class="sourceLineNo">1793</span>     * &lt;p&gt;<a name="line.1793"></a>
<span class="sourceLineNo">1794</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1794"></a>
<span class="sourceLineNo">1795</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary, prior to<a name="line.1795"></a>
<span class="sourceLineNo">1796</span>     * writing the field.<a name="line.1796"></a>
<span class="sourceLineNo">1797</span>     * &lt;/p&gt;<a name="line.1797"></a>
<span class="sourceLineNo">1798</span>     *<a name="line.1798"></a>
<span class="sourceLineNo">1799</span>     * @param id object ID of the object<a name="line.1799"></a>
<span class="sourceLineNo">1800</span>     * @param storageId storage ID of the {@link CounterField}<a name="line.1800"></a>
<span class="sourceLineNo">1801</span>     * @param offset offset value to add to counter value<a name="line.1801"></a>
<span class="sourceLineNo">1802</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1802"></a>
<span class="sourceLineNo">1803</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1803"></a>
<span class="sourceLineNo">1804</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1804"></a>
<span class="sourceLineNo">1805</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1805"></a>
<span class="sourceLineNo">1806</span>     * @throws UnknownFieldException if no {@link CounterField} corresponding to {@code storageId} exists in the object<a name="line.1806"></a>
<span class="sourceLineNo">1807</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1807"></a>
<span class="sourceLineNo">1808</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1808"></a>
<span class="sourceLineNo">1809</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1809"></a>
<span class="sourceLineNo">1810</span>     */<a name="line.1810"></a>
<span class="sourceLineNo">1811</span>    public synchronized void adjustCounterField(ObjId id, int storageId, long offset, boolean updateVersion) {<a name="line.1811"></a>
<span class="sourceLineNo">1812</span><a name="line.1812"></a>
<span class="sourceLineNo">1813</span>        // Sanity check<a name="line.1813"></a>
<span class="sourceLineNo">1814</span>        if (id == null)<a name="line.1814"></a>
<span class="sourceLineNo">1815</span>            throw new IllegalArgumentException("null id");<a name="line.1815"></a>
<span class="sourceLineNo">1816</span>        if (this.stale)<a name="line.1816"></a>
<span class="sourceLineNo">1817</span>            throw new StaleTransactionException(this);<a name="line.1817"></a>
<span class="sourceLineNo">1818</span>        if (this.readOnly)<a name="line.1818"></a>
<span class="sourceLineNo">1819</span>            throw new ReadOnlyTransactionException(this);<a name="line.1819"></a>
<span class="sourceLineNo">1820</span><a name="line.1820"></a>
<span class="sourceLineNo">1821</span>        // Optimize away non-change<a name="line.1821"></a>
<span class="sourceLineNo">1822</span>        if (offset == 0)<a name="line.1822"></a>
<span class="sourceLineNo">1823</span>            return;<a name="line.1823"></a>
<span class="sourceLineNo">1824</span><a name="line.1824"></a>
<span class="sourceLineNo">1825</span>        // Get object info<a name="line.1825"></a>
<span class="sourceLineNo">1826</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1826"></a>
<span class="sourceLineNo">1827</span><a name="line.1827"></a>
<span class="sourceLineNo">1828</span>        // Find field<a name="line.1828"></a>
<span class="sourceLineNo">1829</span>        final CounterField field = info.getObjType().counterFields.get(storageId);<a name="line.1829"></a>
<span class="sourceLineNo">1830</span>        if (field == null)<a name="line.1830"></a>
<span class="sourceLineNo">1831</span>            throw new UnknownFieldException(info.getObjType(), storageId, "counter field");<a name="line.1831"></a>
<span class="sourceLineNo">1832</span><a name="line.1832"></a>
<span class="sourceLineNo">1833</span>        // Adjust counter value<a name="line.1833"></a>
<span class="sourceLineNo">1834</span>        this.kvt.adjustCounter(field.buildKey(id), offset);<a name="line.1834"></a>
<span class="sourceLineNo">1835</span>    }<a name="line.1835"></a>
<span class="sourceLineNo">1836</span><a name="line.1836"></a>
<span class="sourceLineNo">1837</span>    /**<a name="line.1837"></a>
<span class="sourceLineNo">1838</span>     * Access a {@link SetField} associated with an object, optionally updating the object's schema version.<a name="line.1838"></a>
<span class="sourceLineNo">1839</span>     *<a name="line.1839"></a>
<span class="sourceLineNo">1840</span>     * &lt;p&gt;<a name="line.1840"></a>
<span class="sourceLineNo">1841</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1841"></a>
<span class="sourceLineNo">1842</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary.<a name="line.1842"></a>
<span class="sourceLineNo">1843</span>     * &lt;/p&gt;<a name="line.1843"></a>
<span class="sourceLineNo">1844</span>     *<a name="line.1844"></a>
<span class="sourceLineNo">1845</span>     * @param id object ID of the object<a name="line.1845"></a>
<span class="sourceLineNo">1846</span>     * @param storageId storage ID of the {@link SetField}<a name="line.1846"></a>
<span class="sourceLineNo">1847</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1847"></a>
<span class="sourceLineNo">1848</span>     * @return set field value<a name="line.1848"></a>
<span class="sourceLineNo">1849</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1849"></a>
<span class="sourceLineNo">1850</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1850"></a>
<span class="sourceLineNo">1851</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1851"></a>
<span class="sourceLineNo">1852</span>     * @throws UnknownFieldException if no {@link SetField} corresponding to {@code storageId} exists in the object<a name="line.1852"></a>
<span class="sourceLineNo">1853</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1853"></a>
<span class="sourceLineNo">1854</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1854"></a>
<span class="sourceLineNo">1855</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1855"></a>
<span class="sourceLineNo">1856</span>     */<a name="line.1856"></a>
<span class="sourceLineNo">1857</span>    public NavigableSet&lt;?&gt; readSetField(ObjId id, int storageId, boolean updateVersion) {<a name="line.1857"></a>
<span class="sourceLineNo">1858</span>        return this.readComplexField(id, storageId, updateVersion, SetField.class, NavigableSet.class);<a name="line.1858"></a>
<span class="sourceLineNo">1859</span>    }<a name="line.1859"></a>
<span class="sourceLineNo">1860</span><a name="line.1860"></a>
<span class="sourceLineNo">1861</span>    /**<a name="line.1861"></a>
<span class="sourceLineNo">1862</span>     * Access a {@link ListField} associated with an object, optionally updating the object's schema version.<a name="line.1862"></a>
<span class="sourceLineNo">1863</span>     *<a name="line.1863"></a>
<span class="sourceLineNo">1864</span>     * &lt;p&gt;<a name="line.1864"></a>
<span class="sourceLineNo">1865</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1865"></a>
<span class="sourceLineNo">1866</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary.<a name="line.1866"></a>
<span class="sourceLineNo">1867</span>     * &lt;/p&gt;<a name="line.1867"></a>
<span class="sourceLineNo">1868</span>     *<a name="line.1868"></a>
<span class="sourceLineNo">1869</span>     * @param id object ID of the object<a name="line.1869"></a>
<span class="sourceLineNo">1870</span>     * @param storageId storage ID of the {@link ListField}<a name="line.1870"></a>
<span class="sourceLineNo">1871</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1871"></a>
<span class="sourceLineNo">1872</span>     * @return list field value<a name="line.1872"></a>
<span class="sourceLineNo">1873</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1873"></a>
<span class="sourceLineNo">1874</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1874"></a>
<span class="sourceLineNo">1875</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1875"></a>
<span class="sourceLineNo">1876</span>     * @throws UnknownFieldException if no {@link ListField} corresponding to {@code storageId} exists in the object<a name="line.1876"></a>
<span class="sourceLineNo">1877</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1877"></a>
<span class="sourceLineNo">1878</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1878"></a>
<span class="sourceLineNo">1879</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1879"></a>
<span class="sourceLineNo">1880</span>     */<a name="line.1880"></a>
<span class="sourceLineNo">1881</span>    public List&lt;?&gt; readListField(ObjId id, int storageId, boolean updateVersion) {<a name="line.1881"></a>
<span class="sourceLineNo">1882</span>        return this.readComplexField(id, storageId, updateVersion, ListField.class, List.class);<a name="line.1882"></a>
<span class="sourceLineNo">1883</span>    }<a name="line.1883"></a>
<span class="sourceLineNo">1884</span><a name="line.1884"></a>
<span class="sourceLineNo">1885</span>    /**<a name="line.1885"></a>
<span class="sourceLineNo">1886</span>     * Access a {@link MapField} associated with an object, optionally updating the object's schema version.<a name="line.1886"></a>
<span class="sourceLineNo">1887</span>     *<a name="line.1887"></a>
<span class="sourceLineNo">1888</span>     * &lt;p&gt;<a name="line.1888"></a>
<span class="sourceLineNo">1889</span>     * If {@code updateVersion} is true, the schema version of the object will be automatically changed to match<a name="line.1889"></a>
<span class="sourceLineNo">1890</span>     * {@linkplain #getSchema() the schema version associated with this transaction}, if necessary.<a name="line.1890"></a>
<span class="sourceLineNo">1891</span>     * &lt;/p&gt;<a name="line.1891"></a>
<span class="sourceLineNo">1892</span>     *<a name="line.1892"></a>
<span class="sourceLineNo">1893</span>     * @param id object ID of the object<a name="line.1893"></a>
<span class="sourceLineNo">1894</span>     * @param storageId storage ID of the {@link MapField}<a name="line.1894"></a>
<span class="sourceLineNo">1895</span>     * @param updateVersion true to first automatically update the object's schema version, false to not change it<a name="line.1895"></a>
<span class="sourceLineNo">1896</span>     * @return map field value<a name="line.1896"></a>
<span class="sourceLineNo">1897</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.1897"></a>
<span class="sourceLineNo">1898</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.1898"></a>
<span class="sourceLineNo">1899</span>     * @throws UnknownTypeException if {@code id} specifies an unknown object type<a name="line.1899"></a>
<span class="sourceLineNo">1900</span>     * @throws UnknownFieldException if no {@link MapField} corresponding to {@code storageId} exists in the object<a name="line.1900"></a>
<span class="sourceLineNo">1901</span>     * @throws TypeNotInSchemaVersionException {@code updateVersion} is true and the object could not be updated because<a name="line.1901"></a>
<span class="sourceLineNo">1902</span>     *   the object's type does not exist in the schema version associated with this transaction<a name="line.1902"></a>
<span class="sourceLineNo">1903</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1903"></a>
<span class="sourceLineNo">1904</span>     */<a name="line.1904"></a>
<span class="sourceLineNo">1905</span>    public NavigableMap&lt;?, ?&gt; readMapField(ObjId id, int storageId, boolean updateVersion) {<a name="line.1905"></a>
<span class="sourceLineNo">1906</span>        return this.readComplexField(id, storageId, updateVersion, MapField.class, NavigableMap.class);<a name="line.1906"></a>
<span class="sourceLineNo">1907</span>    }<a name="line.1907"></a>
<span class="sourceLineNo">1908</span><a name="line.1908"></a>
<span class="sourceLineNo">1909</span>    /**<a name="line.1909"></a>
<span class="sourceLineNo">1910</span>     * Get the {@code byte[]} key in the underlying key/value store corresponding to the specified object.<a name="line.1910"></a>
<span class="sourceLineNo">1911</span>     *<a name="line.1911"></a>
<span class="sourceLineNo">1912</span>     * &lt;p&gt;<a name="line.1912"></a>
<span class="sourceLineNo">1913</span>     * Notes:<a name="line.1913"></a>
<span class="sourceLineNo">1914</span>     * &lt;ul&gt;<a name="line.1914"></a>
<span class="sourceLineNo">1915</span>     *  &lt;li&gt;This method does not check whether {@code id} is valid or the object actually exists.&lt;/li&gt;<a name="line.1915"></a>
<span class="sourceLineNo">1916</span>     *  &lt;li&gt;Objects utilize mutiple keys; the return value is the common prefix of all such keys.&lt;/li&gt;<a name="line.1916"></a>
<span class="sourceLineNo">1917</span>     *  &lt;li&gt;The {@link org.jsimpledb.kv.KVDatabase} should not be modified directly, otherwise behavior is undefined&lt;/li&gt;<a name="line.1917"></a>
<span class="sourceLineNo">1918</span>     * &lt;/ul&gt;<a name="line.1918"></a>
<span class="sourceLineNo">1919</span>     *<a name="line.1919"></a>
<span class="sourceLineNo">1920</span>     * @param id object ID<a name="line.1920"></a>
<span class="sourceLineNo">1921</span>     * @return the {@link org.jsimpledb.kv.KVDatabase} key corresponding to {@code id}<a name="line.1921"></a>
<span class="sourceLineNo">1922</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1922"></a>
<span class="sourceLineNo">1923</span>     */<a name="line.1923"></a>
<span class="sourceLineNo">1924</span>    public byte[] getKey(ObjId id) {<a name="line.1924"></a>
<span class="sourceLineNo">1925</span>        if (id == null)<a name="line.1925"></a>
<span class="sourceLineNo">1926</span>            throw new IllegalArgumentException("null id");<a name="line.1926"></a>
<span class="sourceLineNo">1927</span>        return id.getBytes();<a name="line.1927"></a>
<span class="sourceLineNo">1928</span>    }<a name="line.1928"></a>
<span class="sourceLineNo">1929</span><a name="line.1929"></a>
<span class="sourceLineNo">1930</span>    /**<a name="line.1930"></a>
<span class="sourceLineNo">1931</span>     * Get the {@code byte[]} key in the underlying key/value store corresponding to the specified field in the specified object.<a name="line.1931"></a>
<span class="sourceLineNo">1932</span>     *<a name="line.1932"></a>
<span class="sourceLineNo">1933</span>     * &lt;p&gt;<a name="line.1933"></a>
<span class="sourceLineNo">1934</span>     * Notes:<a name="line.1934"></a>
<span class="sourceLineNo">1935</span>     * &lt;ul&gt;<a name="line.1935"></a>
<span class="sourceLineNo">1936</span>     *  &lt;li&gt;This method does not check whether {@code id} is valid, the object exists,<a name="line.1936"></a>
<span class="sourceLineNo">1937</span>     *      or the field actually exists in the object.&lt;/li&gt;<a name="line.1937"></a>
<span class="sourceLineNo">1938</span>     *  &lt;li&gt;Complex fields utilize mutiple keys; the return value is the common prefix of all such keys.&lt;/li&gt;<a name="line.1938"></a>
<span class="sourceLineNo">1939</span>     *  &lt;li&gt;The {@link org.jsimpledb.kv.KVDatabase} should not be modified directly, otherwise behavior is undefined&lt;/li&gt;<a name="line.1939"></a>
<span class="sourceLineNo">1940</span>     * &lt;/ul&gt;<a name="line.1940"></a>
<span class="sourceLineNo">1941</span>     *<a name="line.1941"></a>
<span class="sourceLineNo">1942</span>     * @param id object ID<a name="line.1942"></a>
<span class="sourceLineNo">1943</span>     * @param storageId field storage ID<a name="line.1943"></a>
<span class="sourceLineNo">1944</span>     * @return the {@link org.jsimpledb.kv.KVDatabase} key of the field in the specified object<a name="line.1944"></a>
<span class="sourceLineNo">1945</span>     * @throws UnknownFieldException if no field corresponding to {@code storageId} exists<a name="line.1945"></a>
<span class="sourceLineNo">1946</span>     * @throws IllegalArgumentException if {@code storageId} corresponds to a sub-field of a complex field<a name="line.1946"></a>
<span class="sourceLineNo">1947</span>     * @throws IllegalArgumentException if {@code storageId} is less than or equal to zero<a name="line.1947"></a>
<span class="sourceLineNo">1948</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.1948"></a>
<span class="sourceLineNo">1949</span>     */<a name="line.1949"></a>
<span class="sourceLineNo">1950</span>    public byte[] getKey(ObjId id, int storageId) {<a name="line.1950"></a>
<span class="sourceLineNo">1951</span><a name="line.1951"></a>
<span class="sourceLineNo">1952</span>        // Sanity check<a name="line.1952"></a>
<span class="sourceLineNo">1953</span>        if (id == null)<a name="line.1953"></a>
<span class="sourceLineNo">1954</span>            throw new IllegalArgumentException("null id");<a name="line.1954"></a>
<span class="sourceLineNo">1955</span>        if (storageId &lt;= 0)<a name="line.1955"></a>
<span class="sourceLineNo">1956</span>            throw new IllegalArgumentException("storageId &lt;= 0");<a name="line.1956"></a>
<span class="sourceLineNo">1957</span>        final FieldStorageInfo info = this.schemas.verifyStorageInfo(storageId, FieldStorageInfo.class);<a name="line.1957"></a>
<span class="sourceLineNo">1958</span>        if (info.isSubField())<a name="line.1958"></a>
<span class="sourceLineNo">1959</span>            throw new IllegalArgumentException("field is a sub-field of a complex field");<a name="line.1959"></a>
<span class="sourceLineNo">1960</span><a name="line.1960"></a>
<span class="sourceLineNo">1961</span>        // Build key<a name="line.1961"></a>
<span class="sourceLineNo">1962</span>        final ByteWriter writer = new ByteWriter();<a name="line.1962"></a>
<span class="sourceLineNo">1963</span>        id.writeTo(writer);<a name="line.1963"></a>
<span class="sourceLineNo">1964</span>        UnsignedIntEncoder.write(writer, storageId);<a name="line.1964"></a>
<span class="sourceLineNo">1965</span>        return writer.getBytes();<a name="line.1965"></a>
<span class="sourceLineNo">1966</span>    }<a name="line.1966"></a>
<span class="sourceLineNo">1967</span><a name="line.1967"></a>
<span class="sourceLineNo">1968</span>    boolean hasDefaultValue(ObjId id, SimpleField&lt;?&gt; field) {<a name="line.1968"></a>
<span class="sourceLineNo">1969</span><a name="line.1969"></a>
<span class="sourceLineNo">1970</span>        // Sanity check<a name="line.1970"></a>
<span class="sourceLineNo">1971</span>        if (this.stale)<a name="line.1971"></a>
<span class="sourceLineNo">1972</span>            throw new StaleTransactionException(this);<a name="line.1972"></a>
<span class="sourceLineNo">1973</span>        if (id == null)<a name="line.1973"></a>
<span class="sourceLineNo">1974</span>            throw new IllegalArgumentException("null id");<a name="line.1974"></a>
<span class="sourceLineNo">1975</span><a name="line.1975"></a>
<span class="sourceLineNo">1976</span>        // Get object info to verify object exists<a name="line.1976"></a>
<span class="sourceLineNo">1977</span>        final ObjInfo info = this.getObjectInfo(id, false);<a name="line.1977"></a>
<span class="sourceLineNo">1978</span><a name="line.1978"></a>
<span class="sourceLineNo">1979</span>        // Check whether non-default value stored in field<a name="line.1979"></a>
<span class="sourceLineNo">1980</span>        return this.kvt.get(field.buildKey(id)) == null;<a name="line.1980"></a>
<span class="sourceLineNo">1981</span>    }<a name="line.1981"></a>
<span class="sourceLineNo">1982</span><a name="line.1982"></a>
<span class="sourceLineNo">1983</span>    private synchronized &lt;F, V&gt; V readComplexField(ObjId id,<a name="line.1983"></a>
<span class="sourceLineNo">1984</span>      int storageId, boolean updateVersion, Class&lt;F&gt; fieldClass, Class&lt;V&gt; valueType) {<a name="line.1984"></a>
<span class="sourceLineNo">1985</span><a name="line.1985"></a>
<span class="sourceLineNo">1986</span>        // Sanity check<a name="line.1986"></a>
<span class="sourceLineNo">1987</span>        if (this.stale)<a name="line.1987"></a>
<span class="sourceLineNo">1988</span>            throw new StaleTransactionException(this);<a name="line.1988"></a>
<span class="sourceLineNo">1989</span>        if (id == null)<a name="line.1989"></a>
<span class="sourceLineNo">1990</span>            throw new IllegalArgumentException("null id");<a name="line.1990"></a>
<span class="sourceLineNo">1991</span><a name="line.1991"></a>
<span class="sourceLineNo">1992</span>        // Get object info<a name="line.1992"></a>
<span class="sourceLineNo">1993</span>        final ObjInfo info = this.getObjectInfo(id, updateVersion);<a name="line.1993"></a>
<span class="sourceLineNo">1994</span><a name="line.1994"></a>
<span class="sourceLineNo">1995</span>        // Get field<a name="line.1995"></a>
<span class="sourceLineNo">1996</span>        final ComplexField&lt;?&gt; field = info.getObjType().complexFields.get(storageId);<a name="line.1996"></a>
<span class="sourceLineNo">1997</span>        if (!fieldClass.isInstance(field))<a name="line.1997"></a>
<span class="sourceLineNo">1998</span>            throw new UnknownFieldException(info.getObjType(), storageId, fieldClass.getSimpleName());<a name="line.1998"></a>
<span class="sourceLineNo">1999</span><a name="line.1999"></a>
<span class="sourceLineNo">2000</span>        // Return view<a name="line.2000"></a>
<span class="sourceLineNo">2001</span>        return valueType.cast(field.getValueInternal(this, id));<a name="line.2001"></a>
<span class="sourceLineNo">2002</span>    }<a name="line.2002"></a>
<span class="sourceLineNo">2003</span><a name="line.2003"></a>
<span class="sourceLineNo">2004</span>    /**<a name="line.2004"></a>
<span class="sourceLineNo">2005</span>     * Read an object's meta-data, updating its schema version it in the process if requested.<a name="line.2005"></a>
<span class="sourceLineNo">2006</span>     *<a name="line.2006"></a>
<span class="sourceLineNo">2007</span>     * @param id object ID of the object<a name="line.2007"></a>
<span class="sourceLineNo">2008</span>     * @param update true to update object's schema version to match this transaction, false to leave it alone<a name="line.2008"></a>
<span class="sourceLineNo">2009</span>     * @return object info<a name="line.2009"></a>
<span class="sourceLineNo">2010</span>     * @throws UnknownTypeException if object ID specifies an unknown object type<a name="line.2010"></a>
<span class="sourceLineNo">2011</span>     * @throws DeletedObjectException if no object with ID equal to {@code id} is found<a name="line.2011"></a>
<span class="sourceLineNo">2012</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.2012"></a>
<span class="sourceLineNo">2013</span>     */<a name="line.2013"></a>
<span class="sourceLineNo">2014</span>    private ObjInfo getObjectInfo(ObjId id, boolean update) {<a name="line.2014"></a>
<span class="sourceLineNo">2015</span><a name="line.2015"></a>
<span class="sourceLineNo">2016</span>        // Check object type<a name="line.2016"></a>
<span class="sourceLineNo">2017</span>        this.schemas.verifyStorageInfo(id.getStorageId(), ObjTypeStorageInfo.class);<a name="line.2017"></a>
<span class="sourceLineNo">2018</span><a name="line.2018"></a>
<span class="sourceLineNo">2019</span>        // Check schema version<a name="line.2019"></a>
<span class="sourceLineNo">2020</span>        final ObjInfo info = new ObjInfo(this, id);<a name="line.2020"></a>
<span class="sourceLineNo">2021</span>        if (!update || info.getSchema() == this.schema)<a name="line.2021"></a>
<span class="sourceLineNo">2022</span>            return info;<a name="line.2022"></a>
<span class="sourceLineNo">2023</span><a name="line.2023"></a>
<span class="sourceLineNo">2024</span>        // Update schema version<a name="line.2024"></a>
<span class="sourceLineNo">2025</span>        this.mutateAndNotify(new Mutation&lt;Void&gt;() {<a name="line.2025"></a>
<span class="sourceLineNo">2026</span>            @Override<a name="line.2026"></a>
<span class="sourceLineNo">2027</span>            public Void mutate() {<a name="line.2027"></a>
<span class="sourceLineNo">2028</span>                Transaction.this.updateVersion(info, Transaction.this.schema);<a name="line.2028"></a>
<span class="sourceLineNo">2029</span>                return null;<a name="line.2029"></a>
<span class="sourceLineNo">2030</span>            }<a name="line.2030"></a>
<span class="sourceLineNo">2031</span>        });<a name="line.2031"></a>
<span class="sourceLineNo">2032</span><a name="line.2032"></a>
<span class="sourceLineNo">2033</span>        // Get updated object info<a name="line.2033"></a>
<span class="sourceLineNo">2034</span>        return new ObjInfo(this, id);<a name="line.2034"></a>
<span class="sourceLineNo">2035</span>    }<a name="line.2035"></a>
<span class="sourceLineNo">2036</span><a name="line.2036"></a>
<span class="sourceLineNo">2037</span>// Field Change Notifications<a name="line.2037"></a>
<span class="sourceLineNo">2038</span><a name="line.2038"></a>
<span class="sourceLineNo">2039</span>    /**<a name="line.2039"></a>
<span class="sourceLineNo">2040</span>     * Monitor for changes within this transaction of the value of the given field, as seen through a path of references.<a name="line.2040"></a>
<span class="sourceLineNo">2041</span>     *<a name="line.2041"></a>
<span class="sourceLineNo">2042</span>     * &lt;p&gt;<a name="line.2042"></a>
<span class="sourceLineNo">2043</span>     * When the specified field is changed, a listener notification will be delivered for each referring object that<a name="line.2043"></a>
<span class="sourceLineNo">2044</span>     * refers to the object containing the changed field through the specified path of references. Notifications are<a name="line.2044"></a>
<span class="sourceLineNo">2045</span>     * delivered at the end of the mutation operation just prior to returning to the caller. If the listener method<a name="line.2045"></a>
<span class="sourceLineNo">2046</span>     * performs additional mutation(s) which are themselves being listened for, those notifications will also be delivered<a name="line.2046"></a>
<span class="sourceLineNo">2047</span>     * prior to the returning to the original caller. Therefore, care must be taken to avoid change notification dependency<a name="line.2047"></a>
<span class="sourceLineNo">2048</span>     * loops when listeners can themselves mutate fields, to avoid infinite loops.<a name="line.2048"></a>
<span class="sourceLineNo">2049</span>     * &lt;/p&gt;<a name="line.2049"></a>
<span class="sourceLineNo">2050</span>     *<a name="line.2050"></a>
<span class="sourceLineNo">2051</span>     * &lt;p&gt;<a name="line.2051"></a>
<span class="sourceLineNo">2052</span>     * If a non-null {@code types} is provided, then only objects whose types have one of the specified storage ID's<a name="line.2052"></a>
<span class="sourceLineNo">2053</span>     * will trigger notifications to {@code listener}.<a name="line.2053"></a>
<span class="sourceLineNo">2054</span>     * &lt;/p&gt;<a name="line.2054"></a>
<span class="sourceLineNo">2055</span>     *<a name="line.2055"></a>
<span class="sourceLineNo">2056</span>     * &lt;p&gt;<a name="line.2056"></a>
<span class="sourceLineNo">2057</span>     * A referring object may refer to the changed object through more than one actual path of references matching {@code path};<a name="line.2057"></a>
<span class="sourceLineNo">2058</span>     * if so, it will still appear only once in the {@link NavigableSet} provided to the listener (this is of course required<a name="line.2058"></a>
<span class="sourceLineNo">2059</span>     * by set semantics).<a name="line.2059"></a>
<span class="sourceLineNo">2060</span>     * &lt;/p&gt;<a name="line.2060"></a>
<span class="sourceLineNo">2061</span>     *<a name="line.2061"></a>
<span class="sourceLineNo">2062</span>     * &lt;p&gt;<a name="line.2062"></a>
<span class="sourceLineNo">2063</span>     * Although the reference back-tracking algorithm does consolidate multiple paths between the same two objects,<a name="line.2063"></a>
<span class="sourceLineNo">2064</span>     * be careful to avoid an explosion of notifications when objects in the {@code path} have a high degree of fan-in.<a name="line.2064"></a>
<span class="sourceLineNo">2065</span>     * &lt;/p&gt;<a name="line.2065"></a>
<span class="sourceLineNo">2066</span>     *<a name="line.2066"></a>
<span class="sourceLineNo">2067</span>     * &lt;p&gt;<a name="line.2067"></a>
<span class="sourceLineNo">2068</span>     * When a {@link ReferenceField} in {@code path} also happens to be the field being changed, then there is ambiguity<a name="line.2068"></a>
<span class="sourceLineNo">2069</span>     * about how the set of referring objects is calculated: does it use the field's value before or after the change?<a name="line.2069"></a>
<span class="sourceLineNo">2070</span>     * This API guarantees that the answer is "after the change"; however, if another listener on the same field is<a name="line.2070"></a>
<span class="sourceLineNo">2071</span>     * invoked before {@code listener} and mutates any reference field(s) in {@code path}, then whether that additional<a name="line.2071"></a>
<span class="sourceLineNo">2072</span>     * change is also be included in the calculation is undefined.<a name="line.2072"></a>
<span class="sourceLineNo">2073</span>     * &lt;/p&gt;<a name="line.2073"></a>
<span class="sourceLineNo">2074</span>     *<a name="line.2074"></a>
<span class="sourceLineNo">2075</span>     * &lt;p&gt;<a name="line.2075"></a>
<span class="sourceLineNo">2076</span>     * Therefore, for consistency, avoid changing any {@link ReferenceField} from within a listener callback when that<a name="line.2076"></a>
<span class="sourceLineNo">2077</span>     * field is also in some other listener's reference path, and both listeners are watching the same field.<a name="line.2077"></a>
<span class="sourceLineNo">2078</span>     * &lt;/p&gt;<a name="line.2078"></a>
<span class="sourceLineNo">2079</span>     *<a name="line.2079"></a>
<span class="sourceLineNo">2080</span>     * @param storageId storage ID of the field to monitor<a name="line.2080"></a>
<span class="sourceLineNo">2081</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2081"></a>
<span class="sourceLineNo">2082</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2082"></a>
<span class="sourceLineNo">2083</span>     * @param listener callback for notifications on changes in value<a name="line.2083"></a>
<span class="sourceLineNo">2084</span>     * @throws UnknownFieldException if no {@link SimpleField} corresponding to {@code storageId} exists<a name="line.2084"></a>
<span class="sourceLineNo">2085</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2085"></a>
<span class="sourceLineNo">2086</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2086"></a>
<span class="sourceLineNo">2087</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2087"></a>
<span class="sourceLineNo">2088</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2088"></a>
<span class="sourceLineNo">2089</span>     */<a name="line.2089"></a>
<span class="sourceLineNo">2090</span>    public synchronized void addSimpleFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2090"></a>
<span class="sourceLineNo">2091</span>      SimpleFieldChangeListener listener) {<a name="line.2091"></a>
<span class="sourceLineNo">2092</span>        this.validateChangeListener(SimpleField.class, storageId, path, listener);<a name="line.2092"></a>
<span class="sourceLineNo">2093</span>        this.getMonitorsForField(storageId, true).add(new FieldMonitor(storageId, path, types, listener));<a name="line.2093"></a>
<span class="sourceLineNo">2094</span>    }<a name="line.2094"></a>
<span class="sourceLineNo">2095</span><a name="line.2095"></a>
<span class="sourceLineNo">2096</span>    /**<a name="line.2096"></a>
<span class="sourceLineNo">2097</span>     * Monitor for changes within this transaction to the specified {@link SetField} as seen through a path of references.<a name="line.2097"></a>
<span class="sourceLineNo">2098</span>     *<a name="line.2098"></a>
<span class="sourceLineNo">2099</span>     * &lt;p&gt;<a name="line.2099"></a>
<span class="sourceLineNo">2100</span>     * See {@link #addSimpleFieldChangeListener addSimpleFieldChangeListener()} for details on how notifications are delivered.<a name="line.2100"></a>
<span class="sourceLineNo">2101</span>     * &lt;/p&gt;<a name="line.2101"></a>
<span class="sourceLineNo">2102</span>     *<a name="line.2102"></a>
<span class="sourceLineNo">2103</span>     * @param storageId storage ID of the field to monitor<a name="line.2103"></a>
<span class="sourceLineNo">2104</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2104"></a>
<span class="sourceLineNo">2105</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2105"></a>
<span class="sourceLineNo">2106</span>     * @param listener callback for notifications on changes in value<a name="line.2106"></a>
<span class="sourceLineNo">2107</span>     * @throws UnknownFieldException if no {@link SetField} corresponding to {@code storageId} exists<a name="line.2107"></a>
<span class="sourceLineNo">2108</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2108"></a>
<span class="sourceLineNo">2109</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2109"></a>
<span class="sourceLineNo">2110</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2110"></a>
<span class="sourceLineNo">2111</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2111"></a>
<span class="sourceLineNo">2112</span>     */<a name="line.2112"></a>
<span class="sourceLineNo">2113</span>    public synchronized void addSetFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2113"></a>
<span class="sourceLineNo">2114</span>      SetFieldChangeListener listener) {<a name="line.2114"></a>
<span class="sourceLineNo">2115</span>        this.validateChangeListener(SetField.class, storageId, path, listener);<a name="line.2115"></a>
<span class="sourceLineNo">2116</span>        this.getMonitorsForField(storageId, true).add(new FieldMonitor(storageId, path, types, listener));<a name="line.2116"></a>
<span class="sourceLineNo">2117</span>    }<a name="line.2117"></a>
<span class="sourceLineNo">2118</span><a name="line.2118"></a>
<span class="sourceLineNo">2119</span>    /**<a name="line.2119"></a>
<span class="sourceLineNo">2120</span>     * Monitor for changes within this transaction to the specified {@link ListField} as seen through a path of references.<a name="line.2120"></a>
<span class="sourceLineNo">2121</span>     *<a name="line.2121"></a>
<span class="sourceLineNo">2122</span>     * &lt;p&gt;<a name="line.2122"></a>
<span class="sourceLineNo">2123</span>     * See {@link #addSimpleFieldChangeListener addSimpleFieldChangeListener()} for details on how notifications are delivered.<a name="line.2123"></a>
<span class="sourceLineNo">2124</span>     * &lt;/p&gt;<a name="line.2124"></a>
<span class="sourceLineNo">2125</span>     *<a name="line.2125"></a>
<span class="sourceLineNo">2126</span>     * @param storageId storage ID of the field to monitor<a name="line.2126"></a>
<span class="sourceLineNo">2127</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2127"></a>
<span class="sourceLineNo">2128</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2128"></a>
<span class="sourceLineNo">2129</span>     * @param listener callback for notifications on changes in value<a name="line.2129"></a>
<span class="sourceLineNo">2130</span>     * @throws UnknownFieldException if no {@link ListField} corresponding to {@code storageId} exists<a name="line.2130"></a>
<span class="sourceLineNo">2131</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2131"></a>
<span class="sourceLineNo">2132</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2132"></a>
<span class="sourceLineNo">2133</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2133"></a>
<span class="sourceLineNo">2134</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2134"></a>
<span class="sourceLineNo">2135</span>     */<a name="line.2135"></a>
<span class="sourceLineNo">2136</span>    public synchronized void addListFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2136"></a>
<span class="sourceLineNo">2137</span>      ListFieldChangeListener listener) {<a name="line.2137"></a>
<span class="sourceLineNo">2138</span>        this.validateChangeListener(ListField.class, storageId, path, listener);<a name="line.2138"></a>
<span class="sourceLineNo">2139</span>        this.getMonitorsForField(storageId, true).add(new FieldMonitor(storageId, path, types, listener));<a name="line.2139"></a>
<span class="sourceLineNo">2140</span>    }<a name="line.2140"></a>
<span class="sourceLineNo">2141</span><a name="line.2141"></a>
<span class="sourceLineNo">2142</span>    /**<a name="line.2142"></a>
<span class="sourceLineNo">2143</span>     * Monitor for changes within this transaction to the specified {@link MapField} as seen through a path of references.<a name="line.2143"></a>
<span class="sourceLineNo">2144</span>     *<a name="line.2144"></a>
<span class="sourceLineNo">2145</span>     * &lt;p&gt;<a name="line.2145"></a>
<span class="sourceLineNo">2146</span>     * See {@link #addSimpleFieldChangeListener addSimpleFieldChangeListener()} for details on how notifications are delivered.<a name="line.2146"></a>
<span class="sourceLineNo">2147</span>     * &lt;/p&gt;<a name="line.2147"></a>
<span class="sourceLineNo">2148</span>     *<a name="line.2148"></a>
<span class="sourceLineNo">2149</span>     * @param storageId storage ID of the field to monitor<a name="line.2149"></a>
<span class="sourceLineNo">2150</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2150"></a>
<span class="sourceLineNo">2151</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2151"></a>
<span class="sourceLineNo">2152</span>     * @param listener callback for notifications on changes in value<a name="line.2152"></a>
<span class="sourceLineNo">2153</span>     * @throws UnknownFieldException if no {@link MapField} corresponding to {@code storageId} exists<a name="line.2153"></a>
<span class="sourceLineNo">2154</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2154"></a>
<span class="sourceLineNo">2155</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2155"></a>
<span class="sourceLineNo">2156</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2156"></a>
<span class="sourceLineNo">2157</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2157"></a>
<span class="sourceLineNo">2158</span>     */<a name="line.2158"></a>
<span class="sourceLineNo">2159</span>    public synchronized void addMapFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2159"></a>
<span class="sourceLineNo">2160</span>      MapFieldChangeListener listener) {<a name="line.2160"></a>
<span class="sourceLineNo">2161</span>        this.validateChangeListener(MapField.class, storageId, path, listener);<a name="line.2161"></a>
<span class="sourceLineNo">2162</span>        this.getMonitorsForField(storageId, true).add(new FieldMonitor(storageId, path, types, listener));<a name="line.2162"></a>
<span class="sourceLineNo">2163</span>    }<a name="line.2163"></a>
<span class="sourceLineNo">2164</span><a name="line.2164"></a>
<span class="sourceLineNo">2165</span>    /**<a name="line.2165"></a>
<span class="sourceLineNo">2166</span>     * Remove a monitor previously added via {@link #addSimpleFieldChangeListener addSimpleFieldChangeListener()}.<a name="line.2166"></a>
<span class="sourceLineNo">2167</span>     *<a name="line.2167"></a>
<span class="sourceLineNo">2168</span>     * @param storageId storage ID of the field to no longer monitor<a name="line.2168"></a>
<span class="sourceLineNo">2169</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2169"></a>
<span class="sourceLineNo">2170</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2170"></a>
<span class="sourceLineNo">2171</span>     * @param listener callback for notifications on changes in value<a name="line.2171"></a>
<span class="sourceLineNo">2172</span>     * @throws UnknownFieldException if no {@link SimpleField} corresponding to {@code storageId} exists<a name="line.2172"></a>
<span class="sourceLineNo">2173</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2173"></a>
<span class="sourceLineNo">2174</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2174"></a>
<span class="sourceLineNo">2175</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2175"></a>
<span class="sourceLineNo">2176</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2176"></a>
<span class="sourceLineNo">2177</span>     */<a name="line.2177"></a>
<span class="sourceLineNo">2178</span>    public synchronized void removeSimpleFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2178"></a>
<span class="sourceLineNo">2179</span>      SimpleFieldChangeListener listener) {<a name="line.2179"></a>
<span class="sourceLineNo">2180</span>        this.validateChangeListener(SimpleField.class, storageId, path, listener);<a name="line.2180"></a>
<span class="sourceLineNo">2181</span>        this.getMonitorsForField(storageId, false).remove(new FieldMonitor(storageId, path, types, listener));<a name="line.2181"></a>
<span class="sourceLineNo">2182</span>    }<a name="line.2182"></a>
<span class="sourceLineNo">2183</span><a name="line.2183"></a>
<span class="sourceLineNo">2184</span>    /**<a name="line.2184"></a>
<span class="sourceLineNo">2185</span>     * Remove a monitor previously added via {@link #addSetFieldChangeListener addSetFieldChangeListener()}.<a name="line.2185"></a>
<span class="sourceLineNo">2186</span>     *<a name="line.2186"></a>
<span class="sourceLineNo">2187</span>     * @param storageId storage ID of the field to no longer monitor<a name="line.2187"></a>
<span class="sourceLineNo">2188</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2188"></a>
<span class="sourceLineNo">2189</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2189"></a>
<span class="sourceLineNo">2190</span>     * @param listener callback for notifications on changes in value<a name="line.2190"></a>
<span class="sourceLineNo">2191</span>     * @throws UnknownFieldException if no {@link SetField} corresponding to {@code storageId} exists<a name="line.2191"></a>
<span class="sourceLineNo">2192</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2192"></a>
<span class="sourceLineNo">2193</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2193"></a>
<span class="sourceLineNo">2194</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2194"></a>
<span class="sourceLineNo">2195</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2195"></a>
<span class="sourceLineNo">2196</span>     */<a name="line.2196"></a>
<span class="sourceLineNo">2197</span>    public synchronized void removeSetFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2197"></a>
<span class="sourceLineNo">2198</span>      SetFieldChangeListener listener) {<a name="line.2198"></a>
<span class="sourceLineNo">2199</span>        this.validateChangeListener(SetField.class, storageId, path, listener);<a name="line.2199"></a>
<span class="sourceLineNo">2200</span>        this.getMonitorsForField(storageId, false).remove(new FieldMonitor(storageId, path, types, listener));<a name="line.2200"></a>
<span class="sourceLineNo">2201</span>    }<a name="line.2201"></a>
<span class="sourceLineNo">2202</span><a name="line.2202"></a>
<span class="sourceLineNo">2203</span>    /**<a name="line.2203"></a>
<span class="sourceLineNo">2204</span>     * Remove a monitor previously added via {@link #addListFieldChangeListener addListFieldChangeListener()}.<a name="line.2204"></a>
<span class="sourceLineNo">2205</span>     *<a name="line.2205"></a>
<span class="sourceLineNo">2206</span>     * @param storageId storage ID of the field to no longer monitor<a name="line.2206"></a>
<span class="sourceLineNo">2207</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2207"></a>
<span class="sourceLineNo">2208</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2208"></a>
<span class="sourceLineNo">2209</span>     * @param listener callback for notifications on changes in value<a name="line.2209"></a>
<span class="sourceLineNo">2210</span>     * @throws UnknownFieldException if no {@link ListField} corresponding to {@code storageId} exists<a name="line.2210"></a>
<span class="sourceLineNo">2211</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2211"></a>
<span class="sourceLineNo">2212</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2212"></a>
<span class="sourceLineNo">2213</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2213"></a>
<span class="sourceLineNo">2214</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2214"></a>
<span class="sourceLineNo">2215</span>     */<a name="line.2215"></a>
<span class="sourceLineNo">2216</span>    public synchronized void removeListFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2216"></a>
<span class="sourceLineNo">2217</span>      ListFieldChangeListener listener) {<a name="line.2217"></a>
<span class="sourceLineNo">2218</span>        this.validateChangeListener(ListField.class, storageId, path, listener);<a name="line.2218"></a>
<span class="sourceLineNo">2219</span>        this.getMonitorsForField(storageId, false).remove(new FieldMonitor(storageId, path, types, listener));<a name="line.2219"></a>
<span class="sourceLineNo">2220</span>    }<a name="line.2220"></a>
<span class="sourceLineNo">2221</span><a name="line.2221"></a>
<span class="sourceLineNo">2222</span>    /**<a name="line.2222"></a>
<span class="sourceLineNo">2223</span>     * Remove a monitor previously added via {@link #addMapFieldChangeListener addMapFieldChangeListener()}.<a name="line.2223"></a>
<span class="sourceLineNo">2224</span>     *<a name="line.2224"></a>
<span class="sourceLineNo">2225</span>     * @param storageId storage ID of the field to no longer monitor<a name="line.2225"></a>
<span class="sourceLineNo">2226</span>     * @param path path of reference fields (represented by storage IDs) through which to monitor field<a name="line.2226"></a>
<span class="sourceLineNo">2227</span>     * @param types set of allowed storage IDs for the changed object, or null for no restriction<a name="line.2227"></a>
<span class="sourceLineNo">2228</span>     * @param listener callback for notifications on changes in value<a name="line.2228"></a>
<span class="sourceLineNo">2229</span>     * @throws UnknownFieldException if no {@link MapField} corresponding to {@code storageId} exists<a name="line.2229"></a>
<span class="sourceLineNo">2230</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2230"></a>
<span class="sourceLineNo">2231</span>     * @throws IllegalArgumentException if {@code storageId} refers to a sub-field of a complex field<a name="line.2231"></a>
<span class="sourceLineNo">2232</span>     * @throws IllegalArgumentException if {@code path} or {@code listener} is null<a name="line.2232"></a>
<span class="sourceLineNo">2233</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2233"></a>
<span class="sourceLineNo">2234</span>     */<a name="line.2234"></a>
<span class="sourceLineNo">2235</span>    public synchronized void removeMapFieldChangeListener(int storageId, int[] path, Iterable&lt;Integer&gt; types,<a name="line.2235"></a>
<span class="sourceLineNo">2236</span>      MapFieldChangeListener listener) {<a name="line.2236"></a>
<span class="sourceLineNo">2237</span>        this.validateChangeListener(MapField.class, storageId, path, listener);<a name="line.2237"></a>
<span class="sourceLineNo">2238</span>        this.getMonitorsForField(storageId, false).remove(new FieldMonitor(storageId, path, types, listener));<a name="line.2238"></a>
<span class="sourceLineNo">2239</span>    }<a name="line.2239"></a>
<span class="sourceLineNo">2240</span><a name="line.2240"></a>
<span class="sourceLineNo">2241</span>    private &lt;T extends Field&lt;?&gt;&gt; void validateChangeListener(Class&lt;T&gt; expectedFieldType,<a name="line.2241"></a>
<span class="sourceLineNo">2242</span>      int storageId, int[] path, Object listener) {<a name="line.2242"></a>
<span class="sourceLineNo">2243</span><a name="line.2243"></a>
<span class="sourceLineNo">2244</span>        // Sanity check<a name="line.2244"></a>
<span class="sourceLineNo">2245</span>        if (this.stale)<a name="line.2245"></a>
<span class="sourceLineNo">2246</span>            throw new StaleTransactionException(this);<a name="line.2246"></a>
<span class="sourceLineNo">2247</span>        if (path == null)<a name="line.2247"></a>
<span class="sourceLineNo">2248</span>            throw new IllegalArgumentException("null path");<a name="line.2248"></a>
<span class="sourceLineNo">2249</span>        if (listener == null)<a name="line.2249"></a>
<span class="sourceLineNo">2250</span>            throw new IllegalArgumentException("null listener");<a name="line.2250"></a>
<span class="sourceLineNo">2251</span><a name="line.2251"></a>
<span class="sourceLineNo">2252</span>        // Get target field info<a name="line.2252"></a>
<span class="sourceLineNo">2253</span>        final FieldStorageInfo fieldInfo = this.schemas.verifyStorageInfo(storageId, SchemaItem.infoTypeFor(expectedFieldType));<a name="line.2253"></a>
<span class="sourceLineNo">2254</span><a name="line.2254"></a>
<span class="sourceLineNo">2255</span>        // Get object parent of target field, and make sure the field is not a sub-field<a name="line.2255"></a>
<span class="sourceLineNo">2256</span>        if (fieldInfo.isSubField()) {<a name="line.2256"></a>
<span class="sourceLineNo">2257</span>            throw new IllegalArgumentException("the field with storage ID "<a name="line.2257"></a>
<span class="sourceLineNo">2258</span>              + storageId + " is a sub-field of a complex field; listeners can only be registered on regular object fields");<a name="line.2258"></a>
<span class="sourceLineNo">2259</span>        }<a name="line.2259"></a>
<span class="sourceLineNo">2260</span><a name="line.2260"></a>
<span class="sourceLineNo">2261</span>        // Verify all fields in the path are reference fields<a name="line.2261"></a>
<span class="sourceLineNo">2262</span>        for (int pathStorageId : path)<a name="line.2262"></a>
<span class="sourceLineNo">2263</span>            this.schemas.verifyStorageInfo(pathStorageId, ReferenceFieldStorageInfo.class);<a name="line.2263"></a>
<span class="sourceLineNo">2264</span>    }<a name="line.2264"></a>
<span class="sourceLineNo">2265</span><a name="line.2265"></a>
<span class="sourceLineNo">2266</span>    private synchronized HashSet&lt;FieldMonitor&gt; getMonitorsForField(int storageId, boolean adding) {<a name="line.2266"></a>
<span class="sourceLineNo">2267</span>        HashSet&lt;FieldMonitor&gt; monitors = this.monitorMap.get(storageId);<a name="line.2267"></a>
<span class="sourceLineNo">2268</span>        if (monitors == null) {<a name="line.2268"></a>
<span class="sourceLineNo">2269</span>            monitors = new HashSet&lt;FieldMonitor&gt;();<a name="line.2269"></a>
<span class="sourceLineNo">2270</span>            if (adding)<a name="line.2270"></a>
<span class="sourceLineNo">2271</span>                this.monitorMap.put(storageId, monitors);<a name="line.2271"></a>
<span class="sourceLineNo">2272</span>        }<a name="line.2272"></a>
<span class="sourceLineNo">2273</span>        return monitors;<a name="line.2273"></a>
<span class="sourceLineNo">2274</span>    }<a name="line.2274"></a>
<span class="sourceLineNo">2275</span><a name="line.2275"></a>
<span class="sourceLineNo">2276</span>    /**<a name="line.2276"></a>
<span class="sourceLineNo">2277</span>     * Add a pending notification for any {@link FieldMonitor}s watching the specified field in the specified object.<a name="line.2277"></a>
<span class="sourceLineNo">2278</span>     * This method assumes only the appropriate type of monitor is registered as a listener on the field<a name="line.2278"></a>
<span class="sourceLineNo">2279</span>     * and that the provided old and new values have the appropriate types.<a name="line.2279"></a>
<span class="sourceLineNo">2280</span>     */<a name="line.2280"></a>
<span class="sourceLineNo">2281</span>    void addFieldChangeNotification(FieldChangeNotifier notifier) {<a name="line.2281"></a>
<span class="sourceLineNo">2282</span><a name="line.2282"></a>
<span class="sourceLineNo">2283</span>        // Does anybody care?<a name="line.2283"></a>
<span class="sourceLineNo">2284</span>        final int storageId = notifier.getStorageId();<a name="line.2284"></a>
<span class="sourceLineNo">2285</span>        HashSet&lt;FieldMonitor&gt; monitors = this.monitorMap.get(storageId);<a name="line.2285"></a>
<span class="sourceLineNo">2286</span>        if (monitors == null || !Iterables.any(monitors, new MonitoredPredicate(notifier.getId(), storageId)))<a name="line.2286"></a>
<span class="sourceLineNo">2287</span>            return;<a name="line.2287"></a>
<span class="sourceLineNo">2288</span><a name="line.2288"></a>
<span class="sourceLineNo">2289</span>        // Add a pending field monitor notification for the specified field<a name="line.2289"></a>
<span class="sourceLineNo">2290</span>        final TreeMap&lt;Integer, ArrayList&lt;FieldChangeNotifier&gt;&gt; pendingNotificationMap = this.pendingNotifications.get();<a name="line.2290"></a>
<span class="sourceLineNo">2291</span>        ArrayList&lt;FieldChangeNotifier&gt; pendingNotificationList = pendingNotificationMap.get(storageId);<a name="line.2291"></a>
<span class="sourceLineNo">2292</span>        if (pendingNotificationList == null) {<a name="line.2292"></a>
<span class="sourceLineNo">2293</span>            pendingNotificationList = new ArrayList&lt;FieldChangeNotifier&gt;(2);<a name="line.2293"></a>
<span class="sourceLineNo">2294</span>            pendingNotificationMap.put(storageId, pendingNotificationList);<a name="line.2294"></a>
<span class="sourceLineNo">2295</span>        }<a name="line.2295"></a>
<span class="sourceLineNo">2296</span>        pendingNotificationList.add(notifier);<a name="line.2296"></a>
<span class="sourceLineNo">2297</span>    }<a name="line.2297"></a>
<span class="sourceLineNo">2298</span><a name="line.2298"></a>
<span class="sourceLineNo">2299</span>    /**<a name="line.2299"></a>
<span class="sourceLineNo">2300</span>     * Determine if there are any monitors watching the specified field.<a name="line.2300"></a>
<span class="sourceLineNo">2301</span>     */<a name="line.2301"></a>
<span class="sourceLineNo">2302</span>    boolean hasFieldMonitor(ObjId id, Field&lt;?&gt; field) {<a name="line.2302"></a>
<span class="sourceLineNo">2303</span>        final HashSet&lt;FieldMonitor&gt; monitors = this.monitorMap.get(field.storageId);<a name="line.2303"></a>
<span class="sourceLineNo">2304</span>        return monitors != null &amp;&amp; Iterables.any(monitors, new MonitoredPredicate(id, field.storageId));<a name="line.2304"></a>
<span class="sourceLineNo">2305</span>    }<a name="line.2305"></a>
<span class="sourceLineNo">2306</span><a name="line.2306"></a>
<span class="sourceLineNo">2307</span>    /**<a name="line.2307"></a>
<span class="sourceLineNo">2308</span>     * Determine if there are any monitors watching any field in the specified type.<a name="line.2308"></a>
<span class="sourceLineNo">2309</span>     */<a name="line.2309"></a>
<span class="sourceLineNo">2310</span>    boolean hasFieldMonitor(ObjType objType) {<a name="line.2310"></a>
<span class="sourceLineNo">2311</span>        for (int storageId : NavigableSets.intersection(objType.fields.navigableKeySet(), this.monitorMap.navigableKeySet())) {<a name="line.2311"></a>
<span class="sourceLineNo">2312</span>            if (Iterables.any(this.monitorMap.get(storageId), new MonitoredPredicate(ObjId.getMin(objType.storageId), storageId)))<a name="line.2312"></a>
<span class="sourceLineNo">2313</span>                return true;<a name="line.2313"></a>
<span class="sourceLineNo">2314</span>        }<a name="line.2314"></a>
<span class="sourceLineNo">2315</span>        return false;<a name="line.2315"></a>
<span class="sourceLineNo">2316</span>    }<a name="line.2316"></a>
<span class="sourceLineNo">2317</span><a name="line.2317"></a>
<span class="sourceLineNo">2318</span>    /**<a name="line.2318"></a>
<span class="sourceLineNo">2319</span>     * Perform some action and, when entirely done (including re-entrant invocation), issue pending notifications to monitors.<a name="line.2319"></a>
<span class="sourceLineNo">2320</span>     *<a name="line.2320"></a>
<span class="sourceLineNo">2321</span>     * @param id object containing the mutated field; will be validated<a name="line.2321"></a>
<span class="sourceLineNo">2322</span>     * @param mutation change to apply<a name="line.2322"></a>
<span class="sourceLineNo">2323</span>     * @throws ReadOnlyTransactionException if this transaction has been {@linkplain #setReadOnly set read-only}<a name="line.2323"></a>
<span class="sourceLineNo">2324</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2324"></a>
<span class="sourceLineNo">2325</span>     * @throws IllegalArgumentException if {@code id} is null<a name="line.2325"></a>
<span class="sourceLineNo">2326</span>     */<a name="line.2326"></a>
<span class="sourceLineNo">2327</span>    synchronized &lt;V&gt; V mutateAndNotify(ObjId id, Mutation&lt;V&gt; mutation) {<a name="line.2327"></a>
<span class="sourceLineNo">2328</span><a name="line.2328"></a>
<span class="sourceLineNo">2329</span>        // Verify object exists<a name="line.2329"></a>
<span class="sourceLineNo">2330</span>        if (id == null)<a name="line.2330"></a>
<span class="sourceLineNo">2331</span>            throw new IllegalArgumentException("null id");<a name="line.2331"></a>
<span class="sourceLineNo">2332</span>        if (this.stale)<a name="line.2332"></a>
<span class="sourceLineNo">2333</span>            throw new StaleTransactionException(this);<a name="line.2333"></a>
<span class="sourceLineNo">2334</span>        if (this.kvt.get(id.getBytes()) == null)<a name="line.2334"></a>
<span class="sourceLineNo">2335</span>            throw new DeletedObjectException(id);<a name="line.2335"></a>
<span class="sourceLineNo">2336</span><a name="line.2336"></a>
<span class="sourceLineNo">2337</span>        // Perform mutation<a name="line.2337"></a>
<span class="sourceLineNo">2338</span>        return this.mutateAndNotify(mutation);<a name="line.2338"></a>
<span class="sourceLineNo">2339</span>    }<a name="line.2339"></a>
<span class="sourceLineNo">2340</span><a name="line.2340"></a>
<span class="sourceLineNo">2341</span>    private synchronized &lt;V&gt; V mutateAndNotify(Mutation&lt;V&gt; mutation) {<a name="line.2341"></a>
<span class="sourceLineNo">2342</span><a name="line.2342"></a>
<span class="sourceLineNo">2343</span>        // Validate transaction<a name="line.2343"></a>
<span class="sourceLineNo">2344</span>        if (this.stale)<a name="line.2344"></a>
<span class="sourceLineNo">2345</span>            throw new StaleTransactionException(this);<a name="line.2345"></a>
<span class="sourceLineNo">2346</span>        if (this.readOnly)<a name="line.2346"></a>
<span class="sourceLineNo">2347</span>            throw new ReadOnlyTransactionException(this);<a name="line.2347"></a>
<span class="sourceLineNo">2348</span><a name="line.2348"></a>
<span class="sourceLineNo">2349</span>        // If re-entrant invocation, we're already set up<a name="line.2349"></a>
<span class="sourceLineNo">2350</span>        if (this.pendingNotifications.get() != null)<a name="line.2350"></a>
<span class="sourceLineNo">2351</span>            return mutation.mutate();<a name="line.2351"></a>
<span class="sourceLineNo">2352</span><a name="line.2352"></a>
<span class="sourceLineNo">2353</span>        // Set up pending report list, perform mutation, and then issue reports<a name="line.2353"></a>
<span class="sourceLineNo">2354</span>        this.pendingNotifications.set(new TreeMap&lt;Integer, ArrayList&lt;FieldChangeNotifier&gt;&gt;());<a name="line.2354"></a>
<span class="sourceLineNo">2355</span>        try {<a name="line.2355"></a>
<span class="sourceLineNo">2356</span>            return mutation.mutate();<a name="line.2356"></a>
<span class="sourceLineNo">2357</span>        } finally {<a name="line.2357"></a>
<span class="sourceLineNo">2358</span>            try {<a name="line.2358"></a>
<span class="sourceLineNo">2359</span>                final TreeMap&lt;Integer, ArrayList&lt;FieldChangeNotifier&gt;&gt; pendingNotificationMap = this.pendingNotifications.get();<a name="line.2359"></a>
<span class="sourceLineNo">2360</span>                while (!pendingNotificationMap.isEmpty()) {<a name="line.2360"></a>
<span class="sourceLineNo">2361</span><a name="line.2361"></a>
<span class="sourceLineNo">2362</span>                    // Get the next field with pending notifications<a name="line.2362"></a>
<span class="sourceLineNo">2363</span>                    final Map.Entry&lt;Integer, ArrayList&lt;FieldChangeNotifier&gt;&gt; entry = pendingNotificationMap.pollFirstEntry();<a name="line.2363"></a>
<span class="sourceLineNo">2364</span>                    final int storageId = entry.getKey();<a name="line.2364"></a>
<span class="sourceLineNo">2365</span><a name="line.2365"></a>
<span class="sourceLineNo">2366</span>                    // For all pending notifications, back-track references and notify all field monitors for the field<a name="line.2366"></a>
<span class="sourceLineNo">2367</span>                    for (FieldChangeNotifier notifier : entry.getValue()) {<a name="line.2367"></a>
<span class="sourceLineNo">2368</span>                        assert notifier.getStorageId() == storageId;<a name="line.2368"></a>
<span class="sourceLineNo">2369</span>                        final ArrayList&lt;FieldMonitor&gt; monitorList = new ArrayList&lt;&gt;(this.getMonitorsForField(storageId, false));<a name="line.2369"></a>
<span class="sourceLineNo">2370</span>                        if (!monitorList.isEmpty())<a name="line.2370"></a>
<span class="sourceLineNo">2371</span>                            this.notifyFieldMonitors(notifier, NavigableSets.singleton(notifier.getId()), monitorList, 0);<a name="line.2371"></a>
<span class="sourceLineNo">2372</span>                    }<a name="line.2372"></a>
<span class="sourceLineNo">2373</span>                }<a name="line.2373"></a>
<span class="sourceLineNo">2374</span>            } finally {<a name="line.2374"></a>
<span class="sourceLineNo">2375</span>                this.pendingNotifications.remove();<a name="line.2375"></a>
<span class="sourceLineNo">2376</span>            }<a name="line.2376"></a>
<span class="sourceLineNo">2377</span>        }<a name="line.2377"></a>
<span class="sourceLineNo">2378</span>    }<a name="line.2378"></a>
<span class="sourceLineNo">2379</span><a name="line.2379"></a>
<span class="sourceLineNo">2380</span>    // Recursively back-track references along monitor paths and notify monitors when we reach the end (i.e., beginning)<a name="line.2380"></a>
<span class="sourceLineNo">2381</span>    private void notifyFieldMonitors(FieldChangeNotifier notifier,<a name="line.2381"></a>
<span class="sourceLineNo">2382</span>      NavigableSet&lt;ObjId&gt; objects, ArrayList&lt;FieldMonitor&gt; monitorList, int step) {<a name="line.2382"></a>
<span class="sourceLineNo">2383</span><a name="line.2383"></a>
<span class="sourceLineNo">2384</span>        // Find the monitors for whom we have completed all the steps in their (inverse) path,<a name="line.2384"></a>
<span class="sourceLineNo">2385</span>        // and group the remaining monitors by their next inverted reference path step.<a name="line.2385"></a>
<span class="sourceLineNo">2386</span>        final HashMap&lt;Integer, ArrayList&lt;FieldMonitor&gt;&gt; remainingMonitorsMap = new HashMap&lt;&gt;();<a name="line.2386"></a>
<span class="sourceLineNo">2387</span>        for (FieldMonitor monitor : monitorList) {<a name="line.2387"></a>
<span class="sourceLineNo">2388</span><a name="line.2388"></a>
<span class="sourceLineNo">2389</span>            // On the first step, apply the monitor's type filter, if any<a name="line.2389"></a>
<span class="sourceLineNo">2390</span>            if (step == 0 &amp;&amp; monitor.types != null &amp;&amp; !monitor.types.contains(notifier.getId().getBytes()))<a name="line.2390"></a>
<span class="sourceLineNo">2391</span>                continue;<a name="line.2391"></a>
<span class="sourceLineNo">2392</span><a name="line.2392"></a>
<span class="sourceLineNo">2393</span>            // Issue notification callback if we have back-tracked through the whole path<a name="line.2393"></a>
<span class="sourceLineNo">2394</span>            if (monitor.path.length == step) {<a name="line.2394"></a>
<span class="sourceLineNo">2395</span>                notifier.notify(this, monitor.listener, monitor.path, objects);<a name="line.2395"></a>
<span class="sourceLineNo">2396</span>                continue;<a name="line.2396"></a>
<span class="sourceLineNo">2397</span>            }<a name="line.2397"></a>
<span class="sourceLineNo">2398</span><a name="line.2398"></a>
<span class="sourceLineNo">2399</span>            // Sort the unfinished monitors by their next (i.e., previous) reference field<a name="line.2399"></a>
<span class="sourceLineNo">2400</span>            final int storageId = monitor.path[monitor.path.length - step - 1];<a name="line.2400"></a>
<span class="sourceLineNo">2401</span>            ArrayList&lt;FieldMonitor&gt; remainingMonitors = remainingMonitorsMap.get(storageId);<a name="line.2401"></a>
<span class="sourceLineNo">2402</span>            if (remainingMonitors == null) {<a name="line.2402"></a>
<span class="sourceLineNo">2403</span>                remainingMonitors = new ArrayList&lt;FieldMonitor&gt;();<a name="line.2403"></a>
<span class="sourceLineNo">2404</span>                remainingMonitorsMap.put(storageId, remainingMonitors);<a name="line.2404"></a>
<span class="sourceLineNo">2405</span>            }<a name="line.2405"></a>
<span class="sourceLineNo">2406</span>            remainingMonitors.add(monitor);<a name="line.2406"></a>
<span class="sourceLineNo">2407</span>        }<a name="line.2407"></a>
<span class="sourceLineNo">2408</span><a name="line.2408"></a>
<span class="sourceLineNo">2409</span>        // Invert references for each group of remaining monitors and recurse<a name="line.2409"></a>
<span class="sourceLineNo">2410</span>        for (Map.Entry&lt;Integer, ArrayList&lt;FieldMonitor&gt;&gt; entry : remainingMonitorsMap.entrySet()) {<a name="line.2410"></a>
<span class="sourceLineNo">2411</span>            final int storageId = entry.getKey();<a name="line.2411"></a>
<span class="sourceLineNo">2412</span><a name="line.2412"></a>
<span class="sourceLineNo">2413</span>            // Gather all objects that refer to any object in our current "objects" set<a name="line.2413"></a>
<span class="sourceLineNo">2414</span>            final ArrayList&lt;NavigableSet&lt;ObjId&gt;&gt; refsList = new ArrayList&lt;&gt;();<a name="line.2414"></a>
<span class="sourceLineNo">2415</span>            for (ObjId object : objects) {<a name="line.2415"></a>
<span class="sourceLineNo">2416</span>                final NavigableSet&lt;ObjId&gt; refs = this.queryReferences(storageId).get(object);<a name="line.2416"></a>
<span class="sourceLineNo">2417</span>                if (refs != null)<a name="line.2417"></a>
<span class="sourceLineNo">2418</span>                    refsList.add(refs);<a name="line.2418"></a>
<span class="sourceLineNo">2419</span>            }<a name="line.2419"></a>
<span class="sourceLineNo">2420</span><a name="line.2420"></a>
<span class="sourceLineNo">2421</span>            // Recurse on the union of those objects<a name="line.2421"></a>
<span class="sourceLineNo">2422</span>            if (!refsList.isEmpty())<a name="line.2422"></a>
<span class="sourceLineNo">2423</span>                this.notifyFieldMonitors(notifier, NavigableSets.union(refsList), entry.getValue(), step + 1);<a name="line.2423"></a>
<span class="sourceLineNo">2424</span>        }<a name="line.2424"></a>
<span class="sourceLineNo">2425</span>    }<a name="line.2425"></a>
<span class="sourceLineNo">2426</span><a name="line.2426"></a>
<span class="sourceLineNo">2427</span>// Reference Path Queries<a name="line.2427"></a>
<span class="sourceLineNo">2428</span><a name="line.2428"></a>
<span class="sourceLineNo">2429</span>    /**<a name="line.2429"></a>
<span class="sourceLineNo">2430</span>     * Find all objects that refer to any object in the given target set through the specified path of references.<a name="line.2430"></a>
<span class="sourceLineNo">2431</span>     *<a name="line.2431"></a>
<span class="sourceLineNo">2432</span>     * @param path path of one or more reference fields (represented by storage IDs) through which to reach the target objects<a name="line.2432"></a>
<span class="sourceLineNo">2433</span>     * @param targetObjects target objects<a name="line.2433"></a>
<span class="sourceLineNo">2434</span>     * @return set of objects that refer to the {@code targetObjects} via {@code path}<a name="line.2434"></a>
<span class="sourceLineNo">2435</span>     * @throws UnknownFieldException if {@code path} contains a storage ID that does not correspond to a {@link ReferenceField}<a name="line.2435"></a>
<span class="sourceLineNo">2436</span>     * @throws IllegalArgumentException if {@code targetObjects} or {@code path} is null<a name="line.2436"></a>
<span class="sourceLineNo">2437</span>     * @throws IllegalArgumentException if {@code path} is empty<a name="line.2437"></a>
<span class="sourceLineNo">2438</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2438"></a>
<span class="sourceLineNo">2439</span>     */<a name="line.2439"></a>
<span class="sourceLineNo">2440</span>    public NavigableSet&lt;ObjId&gt; invertReferencePath(int[] path, Iterable&lt;ObjId&gt; targetObjects) {<a name="line.2440"></a>
<span class="sourceLineNo">2441</span><a name="line.2441"></a>
<span class="sourceLineNo">2442</span>        // Sanity check<a name="line.2442"></a>
<span class="sourceLineNo">2443</span>        if (targetObjects == null)<a name="line.2443"></a>
<span class="sourceLineNo">2444</span>            throw new IllegalArgumentException("null targetObjects");<a name="line.2444"></a>
<span class="sourceLineNo">2445</span>        if (path == null)<a name="line.2445"></a>
<span class="sourceLineNo">2446</span>            throw new IllegalArgumentException("null path");<a name="line.2446"></a>
<span class="sourceLineNo">2447</span>        if (path.length == 0)<a name="line.2447"></a>
<span class="sourceLineNo">2448</span>            throw new IllegalArgumentException("empty path");<a name="line.2448"></a>
<span class="sourceLineNo">2449</span><a name="line.2449"></a>
<span class="sourceLineNo">2450</span>        // Verify all fields in the path are reference fields<a name="line.2450"></a>
<span class="sourceLineNo">2451</span>        for (int storageId : path)<a name="line.2451"></a>
<span class="sourceLineNo">2452</span>            this.schemas.verifyStorageInfo(storageId, ReferenceFieldStorageInfo.class);<a name="line.2452"></a>
<span class="sourceLineNo">2453</span><a name="line.2453"></a>
<span class="sourceLineNo">2454</span>        // Invert references in reverse order<a name="line.2454"></a>
<span class="sourceLineNo">2455</span>        NavigableSet&lt;ObjId&gt; result = null;<a name="line.2455"></a>
<span class="sourceLineNo">2456</span>        for (int i = path.length - 1; i &gt;= 0; i--) {<a name="line.2456"></a>
<span class="sourceLineNo">2457</span>            final int storageId = path[i];<a name="line.2457"></a>
<span class="sourceLineNo">2458</span><a name="line.2458"></a>
<span class="sourceLineNo">2459</span>            // Gather all objects that refer to any object in our current target objects set<a name="line.2459"></a>
<span class="sourceLineNo">2460</span>            final ArrayList&lt;NavigableSet&lt;ObjId&gt;&gt; refsList = new ArrayList&lt;&gt;();<a name="line.2460"></a>
<span class="sourceLineNo">2461</span>            for (ObjId id : targetObjects) {<a name="line.2461"></a>
<span class="sourceLineNo">2462</span>                final NavigableSet&lt;ObjId&gt; refs = this.queryReferences(storageId).get(id);<a name="line.2462"></a>
<span class="sourceLineNo">2463</span>                if (refs != null)<a name="line.2463"></a>
<span class="sourceLineNo">2464</span>                    refsList.add(refs);<a name="line.2464"></a>
<span class="sourceLineNo">2465</span>            }<a name="line.2465"></a>
<span class="sourceLineNo">2466</span>            if (refsList.isEmpty())<a name="line.2466"></a>
<span class="sourceLineNo">2467</span>                return NavigableSets.empty();<a name="line.2467"></a>
<span class="sourceLineNo">2468</span><a name="line.2468"></a>
<span class="sourceLineNo">2469</span>            // Recurse on the union of those objects<a name="line.2469"></a>
<span class="sourceLineNo">2470</span>            targetObjects = result = NavigableSets.union(refsList);<a name="line.2470"></a>
<span class="sourceLineNo">2471</span>        }<a name="line.2471"></a>
<span class="sourceLineNo">2472</span><a name="line.2472"></a>
<span class="sourceLineNo">2473</span>        // Done<a name="line.2473"></a>
<span class="sourceLineNo">2474</span>        return result;<a name="line.2474"></a>
<span class="sourceLineNo">2475</span>    }<a name="line.2475"></a>
<span class="sourceLineNo">2476</span><a name="line.2476"></a>
<span class="sourceLineNo">2477</span>// Index Queries<a name="line.2477"></a>
<span class="sourceLineNo">2478</span><a name="line.2478"></a>
<span class="sourceLineNo">2479</span>    /**<a name="line.2479"></a>
<span class="sourceLineNo">2480</span>     * Find all values stored in the specified {@link SimpleField} and, for each value,<a name="line.2480"></a>
<span class="sourceLineNo">2481</span>     * the set of all objects having that value in the field.<a name="line.2481"></a>
<span class="sourceLineNo">2482</span>     *<a name="line.2482"></a>
<span class="sourceLineNo">2483</span>     * &lt;p&gt;<a name="line.2483"></a>
<span class="sourceLineNo">2484</span>     * The {@code storageId} may refer to any {@link SimpleField}, whether part of an object or a sub-field<a name="line.2484"></a>
<span class="sourceLineNo">2485</span>     * of a {@link ComplexField}.<a name="line.2485"></a>
<span class="sourceLineNo">2486</span>     * &lt;/p&gt;<a name="line.2486"></a>
<span class="sourceLineNo">2487</span>     *<a name="line.2487"></a>
<span class="sourceLineNo">2488</span>     * &lt;p&gt;<a name="line.2488"></a>
<span class="sourceLineNo">2489</span>     * The returned index contains objects from all recorded schema versions for which the field is indexed;<a name="line.2489"></a>
<span class="sourceLineNo">2490</span>     * this method does not check whether any such schema versions exist.<a name="line.2490"></a>
<span class="sourceLineNo">2491</span>     * &lt;/p&gt;<a name="line.2491"></a>
<span class="sourceLineNo">2492</span>     *<a name="line.2492"></a>
<span class="sourceLineNo">2493</span>     * &lt;p&gt;<a name="line.2493"></a>
<span class="sourceLineNo">2494</span>     * Only objects having schema versions in which the field is indexed will be found;<a name="line.2494"></a>
<span class="sourceLineNo">2495</span>     * this method does not check whether any such schema versions exist.<a name="line.2495"></a>
<span class="sourceLineNo">2496</span>     * &lt;/p&gt;<a name="line.2496"></a>
<span class="sourceLineNo">2497</span>     *<a name="line.2497"></a>
<span class="sourceLineNo">2498</span>     * @param storageId {@link SimpleField}'s storage ID<a name="line.2498"></a>
<span class="sourceLineNo">2499</span>     * @return read-only, real-time view of field values mapped to sets of objects with the value in the field<a name="line.2499"></a>
<span class="sourceLineNo">2500</span>     * @throws UnknownFieldException if no {@link SimpleField} corresponding to {@code storageId} exists<a name="line.2500"></a>
<span class="sourceLineNo">2501</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2501"></a>
<span class="sourceLineNo">2502</span>     */<a name="line.2502"></a>
<span class="sourceLineNo">2503</span>    public synchronized CoreIndex&lt;?, ObjId&gt; queryIndex(int storageId) {<a name="line.2503"></a>
<span class="sourceLineNo">2504</span>        if (this.stale)<a name="line.2504"></a>
<span class="sourceLineNo">2505</span>            throw new StaleTransactionException(this);<a name="line.2505"></a>
<span class="sourceLineNo">2506</span>        final SimpleFieldStorageInfo&lt;?&gt; fieldInfo = this.schemas.verifyStorageInfo(storageId, SimpleFieldStorageInfo.class);<a name="line.2506"></a>
<span class="sourceLineNo">2507</span>        if (fieldInfo.superFieldStorageId == 0)<a name="line.2507"></a>
<span class="sourceLineNo">2508</span>            return fieldInfo.getSimpleFieldIndex(this);<a name="line.2508"></a>
<span class="sourceLineNo">2509</span>        final ComplexFieldStorageInfo&lt;?&gt; superFieldInfo<a name="line.2509"></a>
<span class="sourceLineNo">2510</span>          = this.schemas.verifyStorageInfo(fieldInfo.superFieldStorageId, ComplexFieldStorageInfo.class);<a name="line.2510"></a>
<span class="sourceLineNo">2511</span>        return superFieldInfo.getSimpleSubFieldIndex(this, fieldInfo);<a name="line.2511"></a>
<span class="sourceLineNo">2512</span>    }<a name="line.2512"></a>
<span class="sourceLineNo">2513</span><a name="line.2513"></a>
<span class="sourceLineNo">2514</span>    /**<a name="line.2514"></a>
<span class="sourceLineNo">2515</span>     * Find all values stored as an element in the specified {@link ListField} and, for each such value,<a name="line.2515"></a>
<span class="sourceLineNo">2516</span>     * the set of all objects having that value as an element in the list and the corresponding list index.<a name="line.2516"></a>
<span class="sourceLineNo">2517</span>     *<a name="line.2517"></a>
<span class="sourceLineNo">2518</span>     * &lt;p&gt;<a name="line.2518"></a>
<span class="sourceLineNo">2519</span>     * The returned index contains objects from all recorded schema versions for which the list element field is indexed;<a name="line.2519"></a>
<span class="sourceLineNo">2520</span>     * this method does not check whether any such schema versions exist.<a name="line.2520"></a>
<span class="sourceLineNo">2521</span>     * &lt;/p&gt;<a name="line.2521"></a>
<span class="sourceLineNo">2522</span>     *<a name="line.2522"></a>
<span class="sourceLineNo">2523</span>     * @param storageId {@link ListField}'s storage ID<a name="line.2523"></a>
<span class="sourceLineNo">2524</span>     * @return read-only, real-time view of list element values, objects with the value in the list, and corresponding indicies<a name="line.2524"></a>
<span class="sourceLineNo">2525</span>     * @throws UnknownFieldException if no {@link ListField} corresponding to {@code storageId} exists<a name="line.2525"></a>
<span class="sourceLineNo">2526</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2526"></a>
<span class="sourceLineNo">2527</span>     */<a name="line.2527"></a>
<span class="sourceLineNo">2528</span>    public synchronized CoreIndex2&lt;?, ObjId, Integer&gt; queryListElementIndex(int storageId) {<a name="line.2528"></a>
<span class="sourceLineNo">2529</span>        if (this.stale)<a name="line.2529"></a>
<span class="sourceLineNo">2530</span>            throw new StaleTransactionException(this);<a name="line.2530"></a>
<span class="sourceLineNo">2531</span>        final ListFieldStorageInfo&lt;?&gt; fieldInfo = this.schemas.verifyStorageInfo(storageId, ListFieldStorageInfo.class);<a name="line.2531"></a>
<span class="sourceLineNo">2532</span>        return fieldInfo.getElementFieldIndex(this);<a name="line.2532"></a>
<span class="sourceLineNo">2533</span>    }<a name="line.2533"></a>
<span class="sourceLineNo">2534</span><a name="line.2534"></a>
<span class="sourceLineNo">2535</span>    /**<a name="line.2535"></a>
<span class="sourceLineNo">2536</span>     * Find all values stored as a value in the specified {@link MapField} and, for each such value,<a name="line.2536"></a>
<span class="sourceLineNo">2537</span>     * the set of all objects having that value as a value in the map and the corresponding key.<a name="line.2537"></a>
<span class="sourceLineNo">2538</span>     *<a name="line.2538"></a>
<span class="sourceLineNo">2539</span>     * &lt;p&gt;<a name="line.2539"></a>
<span class="sourceLineNo">2540</span>     * The returned index contains objects from all recorded schema versions for which the map value field is indexed;<a name="line.2540"></a>
<span class="sourceLineNo">2541</span>     * this method does not check whether any such schema versions exist.<a name="line.2541"></a>
<span class="sourceLineNo">2542</span>     * &lt;/p&gt;<a name="line.2542"></a>
<span class="sourceLineNo">2543</span>     *<a name="line.2543"></a>
<span class="sourceLineNo">2544</span>     * @param storageId {@link MapField}'s storage ID<a name="line.2544"></a>
<span class="sourceLineNo">2545</span>     * @return read-only, real-time view of map values, objects with the value in the map, and corresponding keys<a name="line.2545"></a>
<span class="sourceLineNo">2546</span>     * @throws UnknownFieldException if no {@link MapField} corresponding to {@code storageId} exists<a name="line.2546"></a>
<span class="sourceLineNo">2547</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2547"></a>
<span class="sourceLineNo">2548</span>     */<a name="line.2548"></a>
<span class="sourceLineNo">2549</span>    public synchronized CoreIndex2&lt;?, ObjId, ?&gt; queryMapValueIndex(int storageId) {<a name="line.2549"></a>
<span class="sourceLineNo">2550</span>        if (this.stale)<a name="line.2550"></a>
<span class="sourceLineNo">2551</span>            throw new StaleTransactionException(this);<a name="line.2551"></a>
<span class="sourceLineNo">2552</span>        final MapFieldStorageInfo&lt;?, ?&gt; fieldInfo = this.schemas.verifyStorageInfo(storageId, MapFieldStorageInfo.class);<a name="line.2552"></a>
<span class="sourceLineNo">2553</span>        return fieldInfo.getValueFieldIndex(this);<a name="line.2553"></a>
<span class="sourceLineNo">2554</span>    }<a name="line.2554"></a>
<span class="sourceLineNo">2555</span><a name="line.2555"></a>
<span class="sourceLineNo">2556</span>    /**<a name="line.2556"></a>
<span class="sourceLineNo">2557</span>     * Access a composite index on two fields.<a name="line.2557"></a>
<span class="sourceLineNo">2558</span>     *<a name="line.2558"></a>
<span class="sourceLineNo">2559</span>     * &lt;p&gt;<a name="line.2559"></a>
<span class="sourceLineNo">2560</span>     * The returned index contains objects from all recorded schema versions in which the composite index is defined.<a name="line.2560"></a>
<span class="sourceLineNo">2561</span>     * &lt;/p&gt;<a name="line.2561"></a>
<span class="sourceLineNo">2562</span>     *<a name="line.2562"></a>
<span class="sourceLineNo">2563</span>     * @param storageId composite index's storage ID<a name="line.2563"></a>
<span class="sourceLineNo">2564</span>     * @return read-only, real-time view of the fields' values and the objects having those values in the fields<a name="line.2564"></a>
<span class="sourceLineNo">2565</span>     * @throws UnknownIndexException if {@code storageID} is unknown or does not correspond to a composite index on two fields<a name="line.2565"></a>
<span class="sourceLineNo">2566</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2566"></a>
<span class="sourceLineNo">2567</span>     */<a name="line.2567"></a>
<span class="sourceLineNo">2568</span>    @SuppressWarnings("unchecked")<a name="line.2568"></a>
<span class="sourceLineNo">2569</span>    public CoreIndex2&lt;?, ?, ObjId&gt; queryCompositeIndex2(int storageId) {<a name="line.2569"></a>
<span class="sourceLineNo">2570</span>        final CompositeIndexStorageInfo indexInfo = this.schemas.verifyStorageInfo(storageId, CompositeIndexStorageInfo.class);<a name="line.2570"></a>
<span class="sourceLineNo">2571</span>        final Object index = indexInfo.getIndex(this);<a name="line.2571"></a>
<span class="sourceLineNo">2572</span>        if (!(index instanceof CoreIndex2)) {<a name="line.2572"></a>
<span class="sourceLineNo">2573</span>            throw new UnknownIndexException(storageId, "the composite index with storage ID " + storageId<a name="line.2573"></a>
<span class="sourceLineNo">2574</span>              + " is on " + indexInfo.fields.size() + " != 2 fields");<a name="line.2574"></a>
<span class="sourceLineNo">2575</span>        }<a name="line.2575"></a>
<span class="sourceLineNo">2576</span>        return (CoreIndex2&lt;?, ?, ObjId&gt;)index;<a name="line.2576"></a>
<span class="sourceLineNo">2577</span>    }<a name="line.2577"></a>
<span class="sourceLineNo">2578</span><a name="line.2578"></a>
<span class="sourceLineNo">2579</span>    /**<a name="line.2579"></a>
<span class="sourceLineNo">2580</span>     * Access a composite index on three fields.<a name="line.2580"></a>
<span class="sourceLineNo">2581</span>     *<a name="line.2581"></a>
<span class="sourceLineNo">2582</span>     * &lt;p&gt;<a name="line.2582"></a>
<span class="sourceLineNo">2583</span>     * The returned index contains objects from all recorded schema versions in which the composite index is defined.<a name="line.2583"></a>
<span class="sourceLineNo">2584</span>     * &lt;/p&gt;<a name="line.2584"></a>
<span class="sourceLineNo">2585</span>     *<a name="line.2585"></a>
<span class="sourceLineNo">2586</span>     * @param storageId composite index's storage ID<a name="line.2586"></a>
<span class="sourceLineNo">2587</span>     * @return read-only, real-time view of the fields' values and the objects having those values in the fields<a name="line.2587"></a>
<span class="sourceLineNo">2588</span>     * @throws UnknownIndexException if {@code storageID} is unknown or does not correspond to a composite index on two fields<a name="line.2588"></a>
<span class="sourceLineNo">2589</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2589"></a>
<span class="sourceLineNo">2590</span>     */<a name="line.2590"></a>
<span class="sourceLineNo">2591</span>    @SuppressWarnings("unchecked")<a name="line.2591"></a>
<span class="sourceLineNo">2592</span>    public CoreIndex3&lt;?, ?, ?, ObjId&gt; queryCompositeIndex3(int storageId) {<a name="line.2592"></a>
<span class="sourceLineNo">2593</span>        final CompositeIndexStorageInfo indexInfo = this.schemas.verifyStorageInfo(storageId, CompositeIndexStorageInfo.class);<a name="line.2593"></a>
<span class="sourceLineNo">2594</span>        final Object index = indexInfo.getIndex(this);<a name="line.2594"></a>
<span class="sourceLineNo">2595</span>        if (!(index instanceof CoreIndex3)) {<a name="line.2595"></a>
<span class="sourceLineNo">2596</span>            throw new UnknownIndexException(storageId, "the composite index with storage ID " + storageId<a name="line.2596"></a>
<span class="sourceLineNo">2597</span>              + " is on " + indexInfo.fields.size() + " != 3 fields");<a name="line.2597"></a>
<span class="sourceLineNo">2598</span>        }<a name="line.2598"></a>
<span class="sourceLineNo">2599</span>        return (CoreIndex3&lt;?, ?, ?, ObjId&gt;)index;<a name="line.2599"></a>
<span class="sourceLineNo">2600</span>    }<a name="line.2600"></a>
<span class="sourceLineNo">2601</span><a name="line.2601"></a>
<span class="sourceLineNo">2602</span>    /**<a name="line.2602"></a>
<span class="sourceLineNo">2603</span>     * Access a composite index on four fields.<a name="line.2603"></a>
<span class="sourceLineNo">2604</span>     *<a name="line.2604"></a>
<span class="sourceLineNo">2605</span>     * &lt;p&gt;<a name="line.2605"></a>
<span class="sourceLineNo">2606</span>     * The returned index contains objects from all recorded schema versions in which the composite index is defined.<a name="line.2606"></a>
<span class="sourceLineNo">2607</span>     * &lt;/p&gt;<a name="line.2607"></a>
<span class="sourceLineNo">2608</span>     *<a name="line.2608"></a>
<span class="sourceLineNo">2609</span>     * @param storageId composite index's storage ID<a name="line.2609"></a>
<span class="sourceLineNo">2610</span>     * @return read-only, real-time view of the fields' values and the objects having those values in the fields<a name="line.2610"></a>
<span class="sourceLineNo">2611</span>     * @throws UnknownIndexException if {@code storageID} is unknown or does not correspond to a composite index on two fields<a name="line.2611"></a>
<span class="sourceLineNo">2612</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2612"></a>
<span class="sourceLineNo">2613</span>     */<a name="line.2613"></a>
<span class="sourceLineNo">2614</span>    @SuppressWarnings("unchecked")<a name="line.2614"></a>
<span class="sourceLineNo">2615</span>    public CoreIndex4&lt;?, ?, ?, ?, ObjId&gt; queryCompositeIndex4(int storageId) {<a name="line.2615"></a>
<span class="sourceLineNo">2616</span>        final CompositeIndexStorageInfo indexInfo = this.schemas.verifyStorageInfo(storageId, CompositeIndexStorageInfo.class);<a name="line.2616"></a>
<span class="sourceLineNo">2617</span>        final Object index = indexInfo.getIndex(this);<a name="line.2617"></a>
<span class="sourceLineNo">2618</span>        if (!(index instanceof CoreIndex4)) {<a name="line.2618"></a>
<span class="sourceLineNo">2619</span>            throw new UnknownIndexException(storageId, "the composite index with storage ID " + storageId<a name="line.2619"></a>
<span class="sourceLineNo">2620</span>              + " is on " + indexInfo.fields.size() + " != 4 fields");<a name="line.2620"></a>
<span class="sourceLineNo">2621</span>        }<a name="line.2621"></a>
<span class="sourceLineNo">2622</span>        return (CoreIndex4&lt;?, ?, ?, ?, ObjId&gt;)index;<a name="line.2622"></a>
<span class="sourceLineNo">2623</span>    }<a name="line.2623"></a>
<span class="sourceLineNo">2624</span><a name="line.2624"></a>
<span class="sourceLineNo">2625</span>    /**<a name="line.2625"></a>
<span class="sourceLineNo">2626</span>     * Access any composite index by storage ID, regardless of the number of fields indexed.<a name="line.2626"></a>
<span class="sourceLineNo">2627</span>     *<a name="line.2627"></a>
<span class="sourceLineNo">2628</span>     * &lt;p&gt;<a name="line.2628"></a>
<span class="sourceLineNo">2629</span>     * The returned index contains objects from all recorded schema versions in which the composite index is defined.<a name="line.2629"></a>
<span class="sourceLineNo">2630</span>     * &lt;/p&gt;<a name="line.2630"></a>
<span class="sourceLineNo">2631</span>     *<a name="line.2631"></a>
<span class="sourceLineNo">2632</span>     * @param storageId composite index's storage ID<a name="line.2632"></a>
<span class="sourceLineNo">2633</span>     * @return read-only, real-time view of the fields' values and the objects having those values in the fields<a name="line.2633"></a>
<span class="sourceLineNo">2634</span>     * @throws UnknownIndexException if {@code storageID} is unknown or does not correspond to a composite index<a name="line.2634"></a>
<span class="sourceLineNo">2635</span>     * @throws StaleTransactionException if this transaction is no longer usable<a name="line.2635"></a>
<span class="sourceLineNo">2636</span>     */<a name="line.2636"></a>
<span class="sourceLineNo">2637</span>    public Object queryCompositeIndex(int storageId) {<a name="line.2637"></a>
<span class="sourceLineNo">2638</span>        final CompositeIndexStorageInfo indexInfo = this.schemas.verifyStorageInfo(storageId, CompositeIndexStorageInfo.class);<a name="line.2638"></a>
<span class="sourceLineNo">2639</span>        return indexInfo.getIndex(this);<a name="line.2639"></a>
<span class="sourceLineNo">2640</span>    }<a name="line.2640"></a>
<span class="sourceLineNo">2641</span><a name="line.2641"></a>
<span class="sourceLineNo">2642</span>    // Query an index on a reference field for referring objects<a name="line.2642"></a>
<span class="sourceLineNo">2643</span>    @SuppressWarnings("unchecked")<a name="line.2643"></a>
<span class="sourceLineNo">2644</span>    private NavigableMap&lt;ObjId, NavigableSet&lt;ObjId&gt;&gt; queryReferences(int storageId) {<a name="line.2644"></a>
<span class="sourceLineNo">2645</span>        assert this.schemas.verifyStorageInfo(storageId, ReferenceFieldStorageInfo.class) != null;<a name="line.2645"></a>
<span class="sourceLineNo">2646</span>        return (NavigableMap&lt;ObjId, NavigableSet&lt;ObjId&gt;&gt;)this.queryIndex(storageId).asMap();<a name="line.2646"></a>
<span class="sourceLineNo">2647</span>    }<a name="line.2647"></a>
<span class="sourceLineNo">2648</span><a name="line.2648"></a>
<span class="sourceLineNo">2649</span>    /**<a name="line.2649"></a>
<span class="sourceLineNo">2650</span>     * Find all objects that refer to the given target object through the/any reference field with the specified<a name="line.2650"></a>
<span class="sourceLineNo">2651</span>     * {@link DeleteAction}.<a name="line.2651"></a>
<span class="sourceLineNo">2652</span>     *<a name="line.2652"></a>
<span class="sourceLineNo">2653</span>     * &lt;p&gt;<a name="line.2653"></a>
<span class="sourceLineNo">2654</span>     * Because different schema versions can have different {@link DeleteAction}'s configured for the<a name="line.2654"></a>
<span class="sourceLineNo">2655</span>     * same field, we have to iterate through each schema version separately.<a name="line.2655"></a>
<span class="sourceLineNo">2656</span>     * &lt;/p&gt;<a name="line.2656"></a>
<span class="sourceLineNo">2657</span>     *<a name="line.2657"></a>
<span class="sourceLineNo">2658</span>     * @param target referred-to object<a name="line.2658"></a>
<span class="sourceLineNo">2659</span>     * @param onDelete {@link DeleteAction} to match<a name="line.2659"></a>
<span class="sourceLineNo">2660</span>     * @param fieldStorageId reference field storage ID, or -1 to match any reference field<a name="line.2660"></a>
<span class="sourceLineNo">2661</span>     */<a name="line.2661"></a>
<span class="sourceLineNo">2662</span>    private NavigableSet&lt;ObjId&gt; findReferrers(ObjId target, DeleteAction onDelete, int fieldStorageId) {<a name="line.2662"></a>
<span class="sourceLineNo">2663</span>        final ArrayList&lt;NavigableSet&lt;ObjId&gt;&gt; refSets = new ArrayList&lt;&gt;();<a name="line.2663"></a>
<span class="sourceLineNo">2664</span>        for (Schema schemaVersion : this.schemas.versions.values()) {<a name="line.2664"></a>
<span class="sourceLineNo">2665</span><a name="line.2665"></a>
<span class="sourceLineNo">2666</span>            // Check whether any object of this version exist; if not, skip<a name="line.2666"></a>
<span class="sourceLineNo">2667</span>            final NavigableSet&lt;ObjId&gt; versionObjects = this.queryVersion().asMap().get(schemaVersion.versionNumber);<a name="line.2667"></a>
<span class="sourceLineNo">2668</span>            if (versionObjects == null)<a name="line.2668"></a>
<span class="sourceLineNo">2669</span>                continue;<a name="line.2669"></a>
<span class="sourceLineNo">2670</span><a name="line.2670"></a>
<span class="sourceLineNo">2671</span>            // Find all reference fields with storage ID matching fieldStorageId (if not -1) and check them.<a name="line.2671"></a>
<span class="sourceLineNo">2672</span>            // Do this separately for each such field in each object type because the fields may have different DeleteAction's.<a name="line.2672"></a>
<span class="sourceLineNo">2673</span>            for (ObjType objType : schemaVersion.objTypeMap.values()) {<a name="line.2673"></a>
<span class="sourceLineNo">2674</span>                for (ReferenceField field : Iterables.filter(objType.getFieldsAndSubFields(), ReferenceField.class)) {<a name="line.2674"></a>
<span class="sourceLineNo">2675</span><a name="line.2675"></a>
<span class="sourceLineNo">2676</span>                    // Check delete action and field<a name="line.2676"></a>
<span class="sourceLineNo">2677</span>                    if (field.onDelete != onDelete || (fieldStorageId != -1 &amp;&amp; field.storageId != fieldStorageId))<a name="line.2677"></a>
<span class="sourceLineNo">2678</span>                        continue;<a name="line.2678"></a>
<span class="sourceLineNo">2679</span><a name="line.2679"></a>
<span class="sourceLineNo">2680</span>                    // Query index on this field, restricting to those only references coming from objType objects<a name="line.2680"></a>
<span class="sourceLineNo">2681</span>                    final NavigableSet&lt;ObjId&gt; refs = this.queryIndex(field.storageId)<a name="line.2681"></a>
<span class="sourceLineNo">2682</span>                      .filter(1, new KeyRanges(ObjId.getKeyRange(objType.storageId))).asMap().get(target);<a name="line.2682"></a>
<span class="sourceLineNo">2683</span>                    if (refs == null)<a name="line.2683"></a>
<span class="sourceLineNo">2684</span>                        continue;<a name="line.2684"></a>
<span class="sourceLineNo">2685</span><a name="line.2685"></a>
<span class="sourceLineNo">2686</span>                    // Restrict further to the specific schema version<a name="line.2686"></a>
<span class="sourceLineNo">2687</span>                    refSets.add(NavigableSets.intersection(versionObjects, refs));<a name="line.2687"></a>
<span class="sourceLineNo">2688</span>                }<a name="line.2688"></a>
<span class="sourceLineNo">2689</span>            }<a name="line.2689"></a>
<span class="sourceLineNo">2690</span>        }<a name="line.2690"></a>
<span class="sourceLineNo">2691</span>        return !refSets.isEmpty() ? NavigableSets.union(refSets) : NavigableSets.empty(FieldTypeRegistry.OBJ_ID);<a name="line.2691"></a>
<span class="sourceLineNo">2692</span>    }<a name="line.2692"></a>
<span class="sourceLineNo">2693</span><a name="line.2693"></a>
<span class="sourceLineNo">2694</span>    private byte[] buildCompositeIndexEntry(ObjId id, CompositeIndex index) {<a name="line.2694"></a>
<span class="sourceLineNo">2695</span>        return Transaction.buildCompositeIndexEntry(this, id, index);<a name="line.2695"></a>
<span class="sourceLineNo">2696</span>    }<a name="line.2696"></a>
<span class="sourceLineNo">2697</span><a name="line.2697"></a>
<span class="sourceLineNo">2698</span>    private static byte[] buildDefaultCompositeIndexEntry(ObjId id, CompositeIndex index) {<a name="line.2698"></a>
<span class="sourceLineNo">2699</span>        return Transaction.buildCompositeIndexEntry(null, id, index);<a name="line.2699"></a>
<span class="sourceLineNo">2700</span>    }<a name="line.2700"></a>
<span class="sourceLineNo">2701</span><a name="line.2701"></a>
<span class="sourceLineNo">2702</span>    private static byte[] buildCompositeIndexEntry(Transaction tx, ObjId id, CompositeIndex index) {<a name="line.2702"></a>
<span class="sourceLineNo">2703</span>        final ByteWriter writer = new ByteWriter();<a name="line.2703"></a>
<span class="sourceLineNo">2704</span>        UnsignedIntEncoder.write(writer, index.storageId);<a name="line.2704"></a>
<span class="sourceLineNo">2705</span>        for (SimpleField&lt;?&gt; field : index.fields) {<a name="line.2705"></a>
<span class="sourceLineNo">2706</span>            final byte[] value = tx != null ? tx.kvt.get(field.buildKey(id)) : null;<a name="line.2706"></a>
<span class="sourceLineNo">2707</span>            writer.write(value != null ? value : field.fieldType.getDefaultValue());<a name="line.2707"></a>
<span class="sourceLineNo">2708</span>        }<a name="line.2708"></a>
<span class="sourceLineNo">2709</span>        id.writeTo(writer);<a name="line.2709"></a>
<span class="sourceLineNo">2710</span>        return writer.getBytes();<a name="line.2710"></a>
<span class="sourceLineNo">2711</span>    }<a name="line.2711"></a>
<span class="sourceLineNo">2712</span><a name="line.2712"></a>
<span class="sourceLineNo">2713</span>// Mutation<a name="line.2713"></a>
<span class="sourceLineNo">2714</span><a name="line.2714"></a>
<span class="sourceLineNo">2715</span>    interface Mutation&lt;V&gt; {<a name="line.2715"></a>
<span class="sourceLineNo">2716</span>        V mutate();<a name="line.2716"></a>
<span class="sourceLineNo">2717</span>    }<a name="line.2717"></a>
<span class="sourceLineNo">2718</span><a name="line.2718"></a>
<span class="sourceLineNo">2719</span>// SimpleFieldChangeNotifier<a name="line.2719"></a>
<span class="sourceLineNo">2720</span><a name="line.2720"></a>
<span class="sourceLineNo">2721</span>    private abstract class SimpleFieldChangeNotifier implements FieldChangeNotifier {<a name="line.2721"></a>
<span class="sourceLineNo">2722</span><a name="line.2722"></a>
<span class="sourceLineNo">2723</span>        final int storageId;<a name="line.2723"></a>
<span class="sourceLineNo">2724</span>        final ObjId id;<a name="line.2724"></a>
<span class="sourceLineNo">2725</span><a name="line.2725"></a>
<span class="sourceLineNo">2726</span>        SimpleFieldChangeNotifier(SimpleField&lt;?&gt; field, ObjId id) {<a name="line.2726"></a>
<span class="sourceLineNo">2727</span>            this.storageId = field.storageId;<a name="line.2727"></a>
<span class="sourceLineNo">2728</span>            this.id = id;<a name="line.2728"></a>
<span class="sourceLineNo">2729</span>        }<a name="line.2729"></a>
<span class="sourceLineNo">2730</span><a name="line.2730"></a>
<span class="sourceLineNo">2731</span>        @Override<a name="line.2731"></a>
<span class="sourceLineNo">2732</span>        public int getStorageId() {<a name="line.2732"></a>
<span class="sourceLineNo">2733</span>            return this.storageId;<a name="line.2733"></a>
<span class="sourceLineNo">2734</span>        }<a name="line.2734"></a>
<span class="sourceLineNo">2735</span><a name="line.2735"></a>
<span class="sourceLineNo">2736</span>        @Override<a name="line.2736"></a>
<span class="sourceLineNo">2737</span>        public ObjId getId() {<a name="line.2737"></a>
<span class="sourceLineNo">2738</span>            return this.id;<a name="line.2738"></a>
<span class="sourceLineNo">2739</span>        }<a name="line.2739"></a>
<span class="sourceLineNo">2740</span><a name="line.2740"></a>
<span class="sourceLineNo">2741</span>        @Override<a name="line.2741"></a>
<span class="sourceLineNo">2742</span>        public void notify(Transaction tx, Object listener, int[] path, NavigableSet&lt;ObjId&gt; referrers) {<a name="line.2742"></a>
<span class="sourceLineNo">2743</span>            this.notify(tx, (SimpleFieldChangeListener)listener, path, referrers);<a name="line.2743"></a>
<span class="sourceLineNo">2744</span>        }<a name="line.2744"></a>
<span class="sourceLineNo">2745</span><a name="line.2745"></a>
<span class="sourceLineNo">2746</span>        abstract void notify(Transaction tx, SimpleFieldChangeListener listener, int[] path, NavigableSet&lt;ObjId&gt; referrers);<a name="line.2746"></a>
<span class="sourceLineNo">2747</span>    }<a name="line.2747"></a>
<span class="sourceLineNo">2748</span><a name="line.2748"></a>
<span class="sourceLineNo">2749</span>// Callback<a name="line.2749"></a>
<span class="sourceLineNo">2750</span><a name="line.2750"></a>
<span class="sourceLineNo">2751</span>    /**<a name="line.2751"></a>
<span class="sourceLineNo">2752</span>     * Callback interface for notification of transaction completion events.<a name="line.2752"></a>
<span class="sourceLineNo">2753</span>     * Callbacks are registered with a transaction via {@link Transaction#addCallback Transaction.addCallback()},<a name="line.2753"></a>
<span class="sourceLineNo">2754</span>     * and are executed in the order registered, in the same thread that just committed (or rolled back) the transaction.<a name="line.2754"></a>
<span class="sourceLineNo">2755</span>     *<a name="line.2755"></a>
<span class="sourceLineNo">2756</span>     * &lt;p&gt;<a name="line.2756"></a>
<span class="sourceLineNo">2757</span>     * Modeled after Spring's {@link org.springframework.transaction.support.TransactionSynchronization} interface.<a name="line.2757"></a>
<span class="sourceLineNo">2758</span>     * &lt;/p&gt;<a name="line.2758"></a>
<span class="sourceLineNo">2759</span>     *<a name="line.2759"></a>
<span class="sourceLineNo">2760</span>     * @see Transaction#addCallback Transaction.addCallback()<a name="line.2760"></a>
<span class="sourceLineNo">2761</span>     */<a name="line.2761"></a>
<span class="sourceLineNo">2762</span>    public interface Callback {<a name="line.2762"></a>
<span class="sourceLineNo">2763</span><a name="line.2763"></a>
<span class="sourceLineNo">2764</span>        /**<a name="line.2764"></a>
<span class="sourceLineNo">2765</span>         * Invoked before transaction commit (and before {@link #beforeCompletion}).<a name="line.2765"></a>
<span class="sourceLineNo">2766</span>         * This method is invoked when a transaction is intended to be committed; it may however still be rolled back.<a name="line.2766"></a>
<span class="sourceLineNo">2767</span>         *<a name="line.2767"></a>
<span class="sourceLineNo">2768</span>         * &lt;p&gt;<a name="line.2768"></a>
<span class="sourceLineNo">2769</span>         * Any exceptions thrown will result in a transaction rollback and be propagated to the caller.<a name="line.2769"></a>
<span class="sourceLineNo">2770</span>         * &lt;/p&gt;<a name="line.2770"></a>
<span class="sourceLineNo">2771</span>         *<a name="line.2771"></a>
<span class="sourceLineNo">2772</span>         * @param readOnly true if the transaction {@linkplain Transaction#isReadOnly is marked read-only}<a name="line.2772"></a>
<span class="sourceLineNo">2773</span>         */<a name="line.2773"></a>
<span class="sourceLineNo">2774</span>        void beforeCommit(boolean readOnly);<a name="line.2774"></a>
<span class="sourceLineNo">2775</span><a name="line.2775"></a>
<span class="sourceLineNo">2776</span>        /**<a name="line.2776"></a>
<span class="sourceLineNo">2777</span>         * Invoked before transaction completion in any case (but after any {@link #beforeCommit beforeCommit()}).<a name="line.2777"></a>
<span class="sourceLineNo">2778</span>         * This method is invoked whether the transaction is going to be committed or rolled back,<a name="line.2778"></a>
<span class="sourceLineNo">2779</span>         * and is invoked even if {@link #beforeCommit beforeCommit()} throws an exception.<a name="line.2779"></a>
<span class="sourceLineNo">2780</span>         * Typically used to clean up resources before transaction completion.<a name="line.2780"></a>
<span class="sourceLineNo">2781</span>         *<a name="line.2781"></a>
<span class="sourceLineNo">2782</span>         * &lt;p&gt;<a name="line.2782"></a>
<span class="sourceLineNo">2783</span>         * Any exceptions thrown will be logged but will &lt;b&gt;not&lt;/b&gt; propagate to the caller.<a name="line.2783"></a>
<span class="sourceLineNo">2784</span>         * &lt;/p&gt;<a name="line.2784"></a>
<span class="sourceLineNo">2785</span>         */<a name="line.2785"></a>
<span class="sourceLineNo">2786</span>        void beforeCompletion();<a name="line.2786"></a>
<span class="sourceLineNo">2787</span><a name="line.2787"></a>
<span class="sourceLineNo">2788</span>        /**<a name="line.2788"></a>
<span class="sourceLineNo">2789</span>         * Invoked after successful transaction commit (and before {@link #afterCompletion afterCompletion()}).<a name="line.2789"></a>
<span class="sourceLineNo">2790</span>         *<a name="line.2790"></a>
<span class="sourceLineNo">2791</span>         * &lt;p&gt;<a name="line.2791"></a>
<span class="sourceLineNo">2792</span>         * Any exceptions thrown will propagate to the caller.<a name="line.2792"></a>
<span class="sourceLineNo">2793</span>         * &lt;/p&gt;<a name="line.2793"></a>
<span class="sourceLineNo">2794</span>         */<a name="line.2794"></a>
<span class="sourceLineNo">2795</span>        void afterCommit();<a name="line.2795"></a>
<span class="sourceLineNo">2796</span><a name="line.2796"></a>
<span class="sourceLineNo">2797</span>        /**<a name="line.2797"></a>
<span class="sourceLineNo">2798</span>         * Invoked after transaction completion (but after any {@link #afterCommit}).<a name="line.2798"></a>
<span class="sourceLineNo">2799</span>         * This method is invoked in any case, whether the transaction was committed or rolled back.<a name="line.2799"></a>
<span class="sourceLineNo">2800</span>         * Typically used to clean up resources after transaction completion.<a name="line.2800"></a>
<span class="sourceLineNo">2801</span>         *<a name="line.2801"></a>
<span class="sourceLineNo">2802</span>         * &lt;p&gt;<a name="line.2802"></a>
<span class="sourceLineNo">2803</span>         * Any exceptions thrown will be logged but will &lt;b&gt;not&lt;/b&gt; propagate to the caller.<a name="line.2803"></a>
<span class="sourceLineNo">2804</span>         * &lt;/p&gt;<a name="line.2804"></a>
<span class="sourceLineNo">2805</span>         *<a name="line.2805"></a>
<span class="sourceLineNo">2806</span>         * @param committed true if transaction was commited, false if transaction was rolled back<a name="line.2806"></a>
<span class="sourceLineNo">2807</span>         */<a name="line.2807"></a>
<span class="sourceLineNo">2808</span>        void afterCompletion(boolean committed);<a name="line.2808"></a>
<span class="sourceLineNo">2809</span>    }<a name="line.2809"></a>
<span class="sourceLineNo">2810</span><a name="line.2810"></a>
<span class="sourceLineNo">2811</span>    /**<a name="line.2811"></a>
<span class="sourceLineNo">2812</span>     * Adapter class for {@link Callback}.<a name="line.2812"></a>
<span class="sourceLineNo">2813</span>     *<a name="line.2813"></a>
<span class="sourceLineNo">2814</span>     * &lt;p&gt;<a name="line.2814"></a>
<span class="sourceLineNo">2815</span>     * All the implementations in this class do nothing.<a name="line.2815"></a>
<span class="sourceLineNo">2816</span>     * &lt;/p&gt;<a name="line.2816"></a>
<span class="sourceLineNo">2817</span>     */<a name="line.2817"></a>
<span class="sourceLineNo">2818</span>    public static class CallbackAdapter implements Callback {<a name="line.2818"></a>
<span class="sourceLineNo">2819</span><a name="line.2819"></a>
<span class="sourceLineNo">2820</span>        @Override<a name="line.2820"></a>
<span class="sourceLineNo">2821</span>        public void beforeCommit(boolean readOnly) {<a name="line.2821"></a>
<span class="sourceLineNo">2822</span>        }<a name="line.2822"></a>
<span class="sourceLineNo">2823</span><a name="line.2823"></a>
<span class="sourceLineNo">2824</span>        @Override<a name="line.2824"></a>
<span class="sourceLineNo">2825</span>        public void beforeCompletion() {<a name="line.2825"></a>
<span class="sourceLineNo">2826</span>        }<a name="line.2826"></a>
<span class="sourceLineNo">2827</span><a name="line.2827"></a>
<span class="sourceLineNo">2828</span>        @Override<a name="line.2828"></a>
<span class="sourceLineNo">2829</span>        public void afterCommit() {<a name="line.2829"></a>
<span class="sourceLineNo">2830</span>        }<a name="line.2830"></a>
<span class="sourceLineNo">2831</span><a name="line.2831"></a>
<span class="sourceLineNo">2832</span>        @Override<a name="line.2832"></a>
<span class="sourceLineNo">2833</span>        public void afterCompletion(boolean committed) {<a name="line.2833"></a>
<span class="sourceLineNo">2834</span>        }<a name="line.2834"></a>
<span class="sourceLineNo">2835</span>    }<a name="line.2835"></a>
<span class="sourceLineNo">2836</span><a name="line.2836"></a>
<span class="sourceLineNo">2837</span>// Predicates &amp; Functions<a name="line.2837"></a>
<span class="sourceLineNo">2838</span><a name="line.2838"></a>
<span class="sourceLineNo">2839</span>    // Matches FieldMonitors who monitor the specified field in the specified object type<a name="line.2839"></a>
<span class="sourceLineNo">2840</span>    private final class MonitoredPredicate implements Predicate&lt;FieldMonitor&gt; {<a name="line.2840"></a>
<span class="sourceLineNo">2841</span><a name="line.2841"></a>
<span class="sourceLineNo">2842</span>        private final ObjId id;<a name="line.2842"></a>
<span class="sourceLineNo">2843</span>        private final int storageId;<a name="line.2843"></a>
<span class="sourceLineNo">2844</span><a name="line.2844"></a>
<span class="sourceLineNo">2845</span>        MonitoredPredicate(ObjId id, int storageId) {<a name="line.2845"></a>
<span class="sourceLineNo">2846</span>            this.id = id;<a name="line.2846"></a>
<span class="sourceLineNo">2847</span>            this.storageId = storageId;<a name="line.2847"></a>
<span class="sourceLineNo">2848</span>        }<a name="line.2848"></a>
<span class="sourceLineNo">2849</span><a name="line.2849"></a>
<span class="sourceLineNo">2850</span>        @Override<a name="line.2850"></a>
<span class="sourceLineNo">2851</span>        public boolean apply(FieldMonitor monitor) {<a name="line.2851"></a>
<span class="sourceLineNo">2852</span>            return monitor.storageId == this.storageId &amp;&amp; (monitor.types == null || monitor.types.contains(this.id.getBytes()));<a name="line.2852"></a>
<span class="sourceLineNo">2853</span>        }<a name="line.2853"></a>
<span class="sourceLineNo">2854</span>    }<a name="line.2854"></a>
<span class="sourceLineNo">2855</span><a name="line.2855"></a>
<span class="sourceLineNo">2856</span>    // Matches ReferenceFields that have cascadeDelete = true<a name="line.2856"></a>
<span class="sourceLineNo">2857</span>    private final class HasCascadeDelete implements Predicate&lt;ReferenceField&gt; {<a name="line.2857"></a>
<span class="sourceLineNo">2858</span><a name="line.2858"></a>
<span class="sourceLineNo">2859</span>        @Override<a name="line.2859"></a>
<span class="sourceLineNo">2860</span>        public boolean apply(ReferenceField field) {<a name="line.2860"></a>
<span class="sourceLineNo">2861</span>            return field.cascadeDelete;<a name="line.2861"></a>
<span class="sourceLineNo">2862</span>        }<a name="line.2862"></a>
<span class="sourceLineNo">2863</span>    }<a name="line.2863"></a>
<span class="sourceLineNo">2864</span>}<a name="line.2864"></a>
<span class="sourceLineNo">2865</span><a name="line.2865"></a>




























































</pre>
</div>
</body>
</html>
